When trying to build some legacy code (with a recent version of boost), I stumbled upon the following problem:
#include <boost/scoped_array.hpp>
bool foo(const boost::scoped_array<int> bar) {
return bar;
}
bool foo2(const boost::scoped_array<int> bar) {
const bool r = bar;
return r;
}
bool foo3(const boost::scoped_array<int> bar) {
return bar ? true : false;
}
The above source will not compile. Both foo
and foo2
are erroneous. To be precise, the expected implicit conversion from scoped_array to bool is not allowed:
➜ /tmp clang++ --std=c++14 testconversion.cpp -o testconversion.o
testconversion.cpp:4:12: error: no viable conversion from returned value of type 'const boost::scoped_array<int>' to function return type 'bool'
return bar;
^~~
testconversion.cpp:8:16: error: no viable conversion from 'const boost::scoped_array<int>' to 'const bool'
const bool r = bar;
This brings up two questions:
- Why are foo and foo2 not valid? the reference explicitly mentions:
when initializing a new object of type T2, including return statement in a function returning T2;
- When was that legal. The legacy code definitely used to build with boost 1.48.0. Was there
- a change in the boost libraries
- a change to the language/compilers