C++ boost::optional::is_initialized()

C++ boost::optional::is_initialized()

The library Boost optional provides the class Boost::optional, which can be used for optional return values. These are return values from functions that may not always return a result.
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.
With is_initialized() you can check whether an object of type boost::optional is not empty. Boost .Optional speaks about initialized and uninitialized objects  hence, the name of the member function is is_initialized().

Header file used: #include<boost/optional.hpp>

Source code:

#include <boost/optional.hpp>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
using boost::optional;

optional<int> get_even_random()
int i =rand();
return optional<int>{i % 2 == 0, i};

int main()
srand(static_cast<unsigned int>(time(0)));
//uses current time as seed for random number generation
optional<int> i = get_even_random();
if (i.is_initialized())
cout << "square root of random number is: "<<sqrt(static_cast<float>(i.get())) << '\n';
return 0;

square root of random number is: 44862.9

The member function get() is equivalent to operator*.