Because of the current implementation (see Implementation Notes), all of the assignment methods:
Can only guarantee the basic
exception safety: The lvalue optional is left uninitialized
if an exception is thrown (any previous value is first
destroyed using On the other hand, the uninitializing methods:
Provide the no-throw guarantee (assuming a no-throw
However, since // // Case 1: Exception thrown during assignment. // T v0(123); optional<T> opt0(v0); try { T v1(456); optional<T> opt1(v1); opt0 = opt1 ; // If no exception was thrown, assignment succeeded. assert( *opt0 == v1 ) ; } catch(...) { // If any exception was thrown, 'opt0' is reset to uninitialized. assert( !opt0 ) ; } // // Case 2: Exception thrown during reset(v) // T v0(123); optional<T> opt(v0); try { T v1(456); opt.reset ( v1 ) ; // If no exception was thrown, reset succeeded. assert( *opt == v1 ) ; } catch(...) { // If any exception was thrown, 'opt' is reset to uninitialized. assert( !opt ) ; } Swap
|