给定以下代码示例:
boost::optional< int > opt;
opt = 12;
int* p( &*opt );
opt = 24;
assert( p == &*opt );
是否可以保证断言将始终有效?
是的,这是一种保证。在T
的boost::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] 删除。
我来说两句