How can I "reset"/"unset" a boost::optional
?
optional<int> x; if( x ) { // We won't hit this since x is uninitialized } x = 3; if( x ) { // Now we will hit this since x has been initialized } // What should I do here to bring x back to uninitialized state? if( x ) { // I don't want to hit this }
A default-constructed boost::optional is empty - it does not contain a value, so you can't call get() on it. You have to initialise it with a valid value: As a side note, you can use -> in place of get() , like this: value->setInt(10);
Boost C++ Libraries Class template optional is a wrapper for representing 'optional' (or 'nullable') objects who may not (yet) contain a valid value. Optional objects offer full value semantics; they are good for passing by value and usage inside STL containers. This is a header-only library.
The expression boost::none denotes an instance of boost::none_t that can be used as the parameter. Example: #include <boost/none.hpp> optional<int> n(boost::none) ; assert ( ! n ) ; optional<T (not a ref)>::optional( T const& v ) Effect: Directly-Constructs an optional.
x = boost::none;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With