std::variant (3) - Linux Manuals
Defined in header <variant>
template <class... Types> (since C++17)
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.
Types - the types that may be stored in this variant. All types must be (possibly cv-qualified) non-array object types.
constructor (public member function)
destructor (public member function)
operator= (public member function)
index (public member function)
valueless_by_exception (public member function)
emplace (public member function)
swap (public member function)
visit calls the provided functor with the arguments held by one or more variants
holds_alternative checks if a variant currently holds a given type
std::get(std::variant) reads the value of the variant given the index or the type (if the type is unique), throws on error
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
operator< compares variant objects as their contained values
operator<= (function template)
std::swap(std::variant) specializes the std::swap algorithm
monostate placeholder type for use as the first alternative in a variant of non-default-constructible types
bad_variant_access exception thrown on invalid accesses to the value of a variant
variant_size obtains the size of the variant's list of alternatives at compile time
variant_size_v (class template) (variable template)
variant_alternative obtains the type of the alternative specified by its index, at compile time
variant_alternative_t (class template) (alias template)
std::hash<std::variant> specializes the std::hash algorithm
variant_npos index of the variant in the invalid state
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
LWG_2901 C++17 specialization of std::uses_allocator provided, but variant can't properly support allocators specialization removed
// Run this code
in_place_index_ in-place construction tag
in_place_t_ (class template)
optional a wrapper that may or may not hold an object
any Objects that hold instances of any CopyConstructible type.