boost::optional对布尔值的隐式投射是否消失了?



我开始移植vc++10/boost 1.48代码库到vc++12/boost 1.57,我得到一个错误,boost::optional不能转换为bool。我以为这是boost::可选的功能,它被删除了吗?

的例子:

bool fizz(){
  boost::optional<int32_t> buzz;
  return buzz;
}

Error   21  error C2440: 'return' : cannot convert from 'boost::optional<int32_t>' to 'bool'

是。Boost 1.55仍然使用安全Bool习惯用法:

// implicit conversion to "bool"
// No-throw
operator unspecified_bool_type() const { return this->safe_bool() ; }

Boost 1.56, Boost 1.57和Boost 1.58现在使用这个宏:

BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()

大致是:

#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

我猜你没有定义BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS -因为你的编译器支持显式转换操作符,你应该保持这种方式!

最新更新