我可以安全地指向重新分配的boost :: optional的数据吗?

朱利安

给定以下代码示例:

boost::optional< int > opt;
opt = 12;
int* p( &*opt );
opt = 24;
assert( p == &*opt );

是否可以保证断言将始终有效?

理查德·霍奇斯

是的,这是一种保证。Tboost::optional<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
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

可以使用realloc安全地重新分配琐碎可复制对象的存储吗?

硬盘已重新分配扇区...但是现在神奇地没有了!我可以相信吗?

我可以安全地访问可能未分配的内存地址吗?

我可以安全地将分配转移到`this`吗?

我可以安全地向“系统保留”分区中分配字母或隐藏东西吗?

我可以安全地组合我的架构吗

重新同步时,可以安全地将数据写入RAID-1阵列吗?

我可以安全地添加新分区吗?

我可以安全地删除该分区吗?

我可以安全地更新Windows 10吗?

我可以安全地移除lubuntu-desktop吗?

我可以安全地禁用软件保护服务吗?

我可以安全地对textarea进行消毒吗

我可以安全地复制vector <array>吗?

我可以安全地从模式1删除表吗?

我可以安全地忽略这些错误吗?

我可以安全地删除/ var / cache吗?

我可以相信必须重新分配扇区的硬盘吗?

我可以重新分配OCaml GC块吗?

我可以从数组的位置重新分配吗?

我可以重新分配/覆盖std :: string吗?

我可以安全地确认我无权访问数据库吗?

我可以在受感染的虚拟机上安全地重新安装操作系统并仍然保留我的数据吗?

我可以在scikit-learn中安全地分配`coef_`和其他估计参数吗?

我可以安全地删除Ubuntu 12.04分区并为Elementary OS使用未分配的空间吗?

std :: string重新分配可以使指向堆的指针无效吗?

可以安全地将std :: vector <std :: complex <boost:multiprecision :: float128 >>(N).data()重新解释为fftwq_complex *吗?

JVM数组可以重新分配()吗?

我可以在我的存储库中安全地使用 observeForever 吗?