From cppreference.com
template<class...Types>classvariant;(since C++17)The class template std::variant represents a type-safe
.
An instance of 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
).
As with unions, if a variant holds a value of some object type T, the T object is
the variant object.
A variant is not permitted to hold references, arrays, or the type void.
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
, 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
can be used to make such variants default-constructible.
A program that instantiates the definition of std::variant with no template arguments is ill-formed. std::variant<std::monostate> can be used instead.
If a program declares an
or
specialization of std::variant, the program is ill-formed, no diagnostic required.
Template parameters
Types - the types that may be stored in this variant. All types must meet the
requirements (in particular, array types and non-object types are not allowed). Member functions
constructs the variant object
(public member function)
destroys the variant, along with its contained value
(public member function)
assigns a variant
(public member function)
Observers
returns the zero-based index of the alternative held by the variant
(public member function)
checks if the variant is in the invalid state
(public member function)
Modifiers
constructs a value in the variant, in place
(public member function)
swaps with another variant
(public member function)
Visitation
(C++26)
calls the provided functor with the argument held by the variant
(public member function)
Non-member functions
(C++17)
calls the provided functor with the arguments held by one or more variants
(function template)
(C++17)
checks if a variant currently holds a given type
(function template)
(C++17)
reads the value of the variant given the index or the type (if the type is unique), throws on error
(function template)
(C++17)
obtains a pointer to the value of a pointed-to variant given the index or the type (if unique), returns null on error
(function template)
operator==operator!=operator<operator<=operator>operator>=operator<=>
(C++17)(C++17)(C++17)(C++17)(C++17)(C++17)(C++20)
compares variant objects as their contained values
(function template)
(C++17)
specializes the
algorithm
(function template)
Helper classes
Helper objects
Notes
macro ValueStdFeature
(C++17)std::variant: a type-safe union
(C++23)
(DR17)
for classes derived from std::variant
(C++23)
(DR20)Fully constexprstd::variant
(C++26)Member
Example
Run this code
#include<cassert>#include<iostream>#include<string>#include<variant>intmain(){std::variant<int,float>v,w;v=42;// v contains intinti=std::get<int>(v);assert(42==i);// succeedsw=std::get<int>(v);w=std::get<0>(v);// same effect as the previous linew=v;// same effect as the previous line// std::get<double>(v); // error: no double in [int, float]// std::get<3>(v); // error: valid index values are 0 and 1try{std::get<float>(w);// w contains int, not float: will throw}catch(conststd::bad_variant_access&ex){std::cout<<ex.what()<<'\n';}usingnamespacestd::literals;std::variant<std::string>x("abc");// converting constructors work when unambiguousx="def";// converting assignment also works when unambiguousstd::variant<std::string,voidconst*>y("abc");// casts to void const* when passed a char const*assert(std::holds_alternative<voidconst*>(y));// succeedsy="xyz"s;assert(std::holds_alternative<std::string>(y));// succeeds}Possible output:
std::get: wrong index for variant Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++17 specialization of
provided,
but variant cannot properly support allocators specialization removed
C++17 a program could declare an explicit or
partial specialization of std::variantthe program is ill-formed in this
case (no diagnostic required)
C++17 the requirement for storage
allocation was confusing the contained object must be
nested within the variant object See also