gpt4 book ai didi

c++ - boost::optional 对 bool 的隐式转换消失了吗?

转载 作者:可可西里 更新时间:2023-11-01 17:39:05 27 4
gpt4 key购买 nike

我开始将 vc++10/boost 1.48 代码库移植到 vc++12/boost 1.57,但我收到一个错误,提示 boost::optional 无法转换为 bool。我以为这是 boost::optional 的一个特性,它被删除了吗?

例子:

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仍然使用 Safe Bool Idiom :

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

Boost 1.56 , Boost 1.57Boost 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 - 因为你的编译器支持显式转换运算符,所以你应该保持这种状态!

关于c++ - boost::optional 对 bool 的隐式转换消失了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30362470/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com