How it works...
std::variant is a class template that models a type-safe union, holding a value of one of its possible alternatives at any given time. In some rare cases, it is possible, though, that a variant object does not store any value. std::variant has a member function called valueless_by_exception() that returns true if the variant does not hold a value, which is possible only in case of an exception during initialization, therefore, the name of the function.
The size of an std::variant object is as large as its largest alternative. A variant does not store additional data. The value stored by the variant is allocated within the memory representation of the object itself.
A variant can hold multiple alternatives of the same type, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access