Given the following code sample:
boost::optional< int > opt;
opt = 12;
int* p( &*opt );
opt = 24;
assert( p == &*opt );
Is there any guarantee that the assert will always be valid?
yes it's a guarantee. The T
of a boost::optional<T>
is a logically a private member of the optional.
the code above is logically equivalent to:
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
}