example1_basic_usage.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <variant>
#include <string>

int main() {
    std::variant<int, float, std::string> v;
    
    v = 42;
    std::cout << std::get<int>(v) << std::endl;
    
    v = 3.14f;
    std::cout << std::get<float>(v) << std::endl;
    
    v = "hello";
    std::cout << std::get<std::string>(v) << std::endl;
    
    return 0;
}
Back to variant