Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Did boost::optional's implicit cast to bool go away?

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'
like image 409
sellsword Avatar asked May 21 '15 00:05

sellsword


1 Answers

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!

like image 117
Barry Avatar answered Sep 22 '22 19:09

Barry