Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a boost::optional back to an uninitialized state?

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 } 
like image 330
Guy Sirton Avatar asked Jan 22 '12 01:01

Guy Sirton


People also ask

How do I initialize a boost optional?

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);

What does boost Optional do?

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.

What is boost none?

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.


1 Answers

x = boost::none; 

like image 132
Benjamin Lindley Avatar answered Sep 28 '22 19:09

Benjamin Lindley