给定以下代码示例:
boost::optional< int > opt;
opt = 12;
int* p( &*opt );
opt = 24;
assert( p == &*opt );
是否有任何保证断言将始终有效?
给定以下代码示例:
boost::optional< int > opt;
opt = 12;
int* p( &*opt );
opt = 24;
assert( p == &*opt );
是否有任何保证断言将始终有效?
是的,这是一个保证。T
a在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
}