I started porting a vc++10/boost 1.48 codebase to vc++12/boost 1.57 and I'm getting an error that boost::optional can't convert to bool. I thought this was a feature of boost::optional, did it get removed?
Example:
bool fizz(){
boost::optional<int32_t> buzz;
return buzz;
}
gives
Error 21 error C2440: 'return' : cannot convert from 'boost::optional<int32_t>' to 'bool'
Yes. Boost 1.55 still used the Safe Bool Idiom:
// implicit conversion to "bool"
// No-throw
operator unspecified_bool_type() const { return this->safe_bool() ; }
Boost 1.56, Boost 1.57 and Boost 1.58 now use this macro:
BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
which is roughly:
#if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
explicit operator bool() const noexcept;
#else if !defined(BOOST_NO_UNSPECIFIED_BOOL)
operator boost::detail::unspecified_bool_type () const noexcept;
#else
operator bool () const noexcept;
#endif
I'm guessing you don't have BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
defined - and since your compiler supports explicit conversion operators, you should probably keep it that way!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With