Boost :: optional <bool> dereference

时间:2018-01-30 13:33:27

标签: c++ boost boolean optional boost-optional

我正在审核一些代码,并且有类似的内容:

RewriteRule

所以我的问题是,最后一行是否与此相当:

boost::optional<bool> isSet = ...;
... some code goes here...
bool smthelse = isSet ? *isSet : false;

2 个答案:

答案 0 :(得分:4)

不,他们不等同。

analytic_account_id表示如果isSet ? *isSet : false;包含值,则获取值,否则返回isSet

BTW:false无法工作,因为bool smthelse = isSet;被声明为operator bool

答案 1 :(得分:4)

这是表格:

boost::optional<bool> isSet | none | true | false |
----------------------------|------|------|-------|
isSet ? *isSet : false;     | false| true | false |
isSet                       | false| true | true  |

正如您在上一栏中看到的不同之处,isSet已被赋予布尔值false

或者,您可以使用isSet.get_value_or(false);