std::variant (3) - Linux Manuals
std::variant: std::variant
NAME
Synopsis
Defined in header <variant>
template <class... Types> (since C++17)
class variant;
The class template std::variant represents a type-safe union. An instance of std::variant at any given time either holds a value of one of its alternative types, or in the case of error - no value (this state is hard to achieve, see valueless_by_exception).
As with unions, if a variant holds a value of some object type T, the object representation of T is allocated directly within the object representation of the variant itself. Variant is not allowed to allocate additional (dynamic) memory.
A variant is not permitted to hold references, arrays, or the type void. Empty variants are also ill-formed (std::variant<std::monostate> can be used instead).
A variant is permitted to hold the same type more than once, and to hold differently cv-qualified versions of the same type.
Consistent with the behavior of unions during aggregate_initialization, a default-constructed variant holds a value of its first alternative, unless that alternative is not default-constructible (in which case the variant is not default-constructible either). The helper class std::monostate can be used to make such variants default-constructible.
Template parameters
Types - the types that may be stored in this variant. All types must be (possibly cv-qualified) non-array object types.
Member functions
constructor (public member function)
destructor (public member function)
operator= (public member function)
Observers
index (public member function)
valueless_by_exception (public member function)
Modifiers
emplace (public member function)
swap (public member function)
Non-member functions
visit calls the provided functor with the arguments held by one or more variants
(C++17)
holds_alternative checks if a variant currently holds a given type
(C++17)
std::get(std::variant) reads the value of the variant given the index or the type (if the type is unique), throws on error
(C++17)
get_if obtains a pointer to the value of a pointed-to variant given the index or the type (if unique), returns null on error