How do you actually take a value out of optional? Meaning take ownership of the value inside the std::optional and replace it with std::nullopt (or swap it with another value)?
In Rust for example you could .unwrap your Option or do something like foo.take().unwrap(). I'm trying to do something like that with C++ optionals.
operator*/value()returns a reference to the value held by theoptional, so you can simply usestd::moveto move it to a temporary variableThis will invoke the rvalue reference overload of
operator*()of theoptional,which returns an rvalue reference to the contained value.You can also directly perform
std::moveon the return value of theoperator*()of the lvalueoptional, which will convert the lvalue reference of the contained value into an rvalue