Basic std::variant use

suggest change

This creates a variant (a tagged union) that can store either an int or a string.

std::variant< int, std::string > var;

We can store one of either type in it:

var = "hello"s;

And we can access the contents via std::visit:

// Prints "hello\n":
visit( [](auto&& e) {
  std::cout << e << '\n';
}, var );

by passing in a polymorphic lambda or similar function object.

If we are certain we know what type it is, we can get it:

auto str = std::get<std::string>(var);

but this will throw if we get it wrong. get_if:

auto* str  = std::get_if<std::string>(&var);

returns nullptr if you guess wrong.

Variants guarantee no dynamic memory allocation (other than which is allocated by their contained types). Only one of the types in a variant is stored there, and in rare cases (involving exceptions while assigning and no safe way to back out) the variant can become empty.

Variants let you store multiple value types in one variable safely and efficiently. They are basically smart, type-safe unions.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


std::variant:
* Basic std::variant use

Table Of Contents
8 Arrays
11 Loops
30 std::variant
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype