升压::可选<bool>取消引用
Boost::optional<bool> dereference
我正在审查一些代码,并有这样的东西:
boost::optional<bool> isSet = ...;
... some code goes here...
bool smthelse = isSet ? *isSet : false;
所以我的问题是,最后一行是否等同于此:
bool smthelse = isSet;
下表如下:
boost::optional<bool> isSet | none | true | false |
----------------------------|------|------|-------|
isSet ? *isSet : false; | false| true | false |
isSet | false| true | true |
正如您在最后一列中看到的差异,其中isSet
被分配了布尔值false
。
或者,您可以使用 isSet.get_value_or(false);
.
不,
它们不等同。
isSet ? *isSet : false;
表示如果isSet
包含值,则获取该值,否则返回 false
。
顺便说一句:bool smthelse = isSet;
不起作用,因为operator bool
被声明为 explicit
.
相关文章:
- C++取消引用指针.为什么会发生变化
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 取消引用运算符不能重载
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- C++ 关于指针取消引用的技术问题
- 没有取消引用/解包对象的标准方法?
- 列表 iter 不取消引用 使用列表进行插入排序
- 在这个函数中是有缺陷的,因为取消引用 null 是无效的,所以我想更改代码
- 取消引用结束指针到数组类型的一个
- 取消引用向量时出现问题
- 取消引用指向整数的指针时获得不同的结果
- C++:取消引用十六进制值,有点语法问题
- C/C++ 取消引用错误:在空检查之前取消引用
- 取消引用后C++空测试
- 立即取消引用unique_ptr
- 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?
- 取消引用 void 指针以将值分配给结构
- boost_multi_index迭代器取消引用给出常量
- 矢量迭代器不能与 std::shared_ptr<> 取消引用
- C++我们可以取消引用此指针吗?如果是这样,那么如何,如果不是,那为什么?