我可以安全地指向重新分配的boost::optional的数据吗?
Can I safely point to the data of a reassigned boost::optional?
给定以下代码示例:
boost::optional< int > opt;
opt = 12;
int* p( &*opt );
opt = 24;
assert( p == &*opt );
是否保证断言总是有效的?
是的,这是一个保证。boost::optional<T>
的T
逻辑上是可选对象的私有成员。
bool opt_constructed = false;
int opt_i; // not constructed
new int (&opt_i)(12); opt_constructed = true; // in-place constructed
int*p = &opt_i;
opt_i = 24;
assert(p == &opt_i);
// destuctor
if (opt_constructed) {
// call opt_i's destructor if it has one
}
相关文章:
- boost 是否有按特殊类型值编码状态"compact optional"?
- 如何正确地将 boost::optional<std::chrono::d uration> 作为函数参数?
- 我已经编写了C++代码将 boost::optional 视为一个范围,但它无法编译
- 将 std::vector<boost::optional<double>> 转换为 std::vector<double>
- 为什么 boost::optional::is_initialized() 被弃用了?
- 如何将构造函数参数转发到 boost::optional
- 如何在 setter 方法中设置 boost::optional 成员变量的值?
- boost::any_cast 和 std::optional's
- 如何将boost::optional设置回未初始化状态
- 我可以安全地指向重新分配的boost::optional的数据吗?
- 使用boost::optional时避免使用temporary
- 从boost::optional中检索对象
- 使用boost::optional失败
- 使用boost::optional捕获和包装异常
- 使用boost::shared_ptr而不是boost::optional的缺点
- 继承虚拟函数的类的boost::optional失败的原因
- 对于最简单的类型,我应该使用boost::optional来表示可以为null的int double等吗
- 将boost::optional与boost::adapter::indirected一起使用
- 带有const成员的Boost::optional
- 为什么不使用boost::optional作为更好的scoped_ptr呢?