C++::Boost::Variant::get( )














































C++::Boost::Variant::get( )



Description - To display the stored values of the  boost::variant  variable, use the free-standing  function boost::get() .

boost::get() expects one of the valid types for the corresponding variable as a template parameter.

Features of Boost::get( ) Function:

Specifying an invalid type will result in a run-time error because validation of types does not take place at compile time.

Variables of type boost::variant can be written to streams such as the standard output stream, bypassing the hazard of run-time errors

Code:

#include <boost/variant.hpp> #include <string> #include <iostream> int main() { boost::variant<double, char, std::string> v; v = 3.14; std::cout << boost::get<double>(v) << '\n'; v = 'A'; std::cout << boost::get<char>(v) << '\n'; v = "Boost"; std::cout << boost::get<std::string>(v) << '\n'; }


Output

14.5
Z
Boost::get() function

Comments