How to move from std::optional<T>
It is valid to move from optional<T>::value()
since it returns a mutable reference and the move does not destroy the object. If the optional
instance is not engaged, value()
will throw a bad_optional_access
exception (§20.6.4.5).
You explicitly check whether the option is engaged:
if (content)
Process(move(*content));
But you don't use the member value()
to access the underlying T
. Note that value()
performs a check internally before returning a valid T&
, unlike operator*
which has a precondition that the optional
instance shall be engaged. This is a subtle difference, but you use the right idiom:
if (o)
f(*o)
as opposed to
if (o) // redundant check
f(o.value())
In Boost, the situation is a little different: first, there exists no member function called value()
that provides checked access. (A bad_optional_access
exception simply does not exist). The member get()
is just an alias for operator*
and always relies on the user checking that the optional
instance is engaged.