
"Fernando Cacciola" <fernando_cacciola@hotmail.com> wrote in message news:cuvnj8$urr$1@sea.gmane.org...
The correct fix along Joe's initial proposal would be:
void assign(optional_base const& rhs) { if (is_initialized) { if ( rhs.is_initialized ) get_impl() = rhs.get_impl(); else destroy(); } else { if ( rhs.is_initialized ) construct(rhs.get_impl()); } }
AFAICS, this code is as safe aliasing-wise as it can be (it handles not only the trivial case of this==&rhs but also any other deeper aliasing issues)
If no voice is raised I'll proceed with this fix.
While you're at it, you should also fix your other versions of assign(). For instance, consider this flavor of self-assignment: optional<string> x("foo"); x = *x; // Assign to x from dereferenced x. This calls the following version of assign: void assign ( argument_type val ) { destroy(); construct(val); } Thus the code destroys *x, then attempts to assign to *x by dereferencing a pointer to uninitialized memory, resulting in undefined behavior. This is should be rewritten as void assign(argument_type val) { if (is_initialized) { get_impl() = val; } else { construct(val); } } Joe Gottman