variant.cpp

The following code example is taken from the book
C++ Templates - The Complete Guide, 2nd Edition
by David Vandevoorde, Nicolai M. Josuttis, and Douglas Gregor,
Addison-Wesley, 2017
© Copyright David Vandevoorde, Nicolai M. Josuttis, Douglas Gregor 2017


#include "variant-all.hpp"
#include <iostream>
#include <string>

int main() 
{
  Variant<int, double, std::string> field(17);
  if (field.is<int>()) {
    std::cout << "Field stores the integer " << field.get<int>()
              << std::endl;
  }
  field = 42;      // assign value of same type
  field = "hello"; // assign value of different type
  std::cout << "Field now stores the string \"" 
            << field.get<std::string>() << '\"' << std::endl;
}