From cppreference.com
This header is part of the
library.
Includes
(C++20)
support
Classes
(C++17)
a type-safe discriminated union
(class template)
(C++17)
placeholder type for use as the first alternative in a variant of non-default-constructible types
(class)
(C++17)
exception thrown on invalid accesses to the value of a variant
(class)
(C++17)
obtains the size of the variant's list of alternatives at compile time
(class template)(variable template)
variant_alternativevariant_alternative_t
(C++17)
obtains the type of the alternative specified by its index, at compile time
(class template)(alias template)
(C++17)
hash support for
(class template specialization)
Constants
(C++17)
index of the variant in the invalid state
(constant)
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)
Synopsis
#include<compare>namespacestd{// class template varianttemplate<class...Types>classvariant;// variant helper classestemplate<classT>structvariant_size;// not definedtemplate<classT>structvariant_size<constT>;template<classT>inlineconstexprsize_tvariant_size_v=variant_size<T>::value;template<class...Types>structvariant_size<variant<Types...>>;template<size_tI,classT>structvariant_alternative;// not definedtemplate<size_tI,classT>structvariant_alternative<I,constT>;template<size_tI,classT>usingvariant_alternative_t=typenamevariant_alternative<I,T>::type;template<size_tI,class...Types>structvariant_alternative<I,variant<Types...>>;inlineconstexprsize_tvariant_npos=-1;// value accesstemplate<classT,class...Types>constexprboolholds_alternative(constvariant<Types...>&)noexcept;template<size_tI,class...Types>constexprvariant_alternative_t<I,variant<Types...>>&get(variant<Types...>&);template<size_tI,class...Types>constexprvariant_alternative_t<I,variant<Types...>>&&get(variant<Types...>&&);template<size_tI,class...Types>constexprconstvariant_alternative_t<I,variant<Types...>>&get(constvariant<Types...>&);template<size_tI,class...Types>constexprconstvariant_alternative_t<I,variant<Types...>>&&get(constvariant<Types...>&&);template<classT,class...Types>constexprT&get(variant<Types...>&);template<classT,class...Types>constexprT&&get(variant<Types...>&&);template<classT,class...Types>constexprconstT&get(constvariant<Types...>&);template<classT,class...Types>constexprconstT&&get(constvariant<Types...>&&);template<size_tI,class...Types>constexpradd_pointer_t<variant_alternative_t<I,variant<Types...>>>get_if(variant<Types...>*)noexcept;template<size_tI,class...Types>constexpradd_pointer_t<constvariant_alternative_t<I,variant<Types...>>>get_if(constvariant<Types...>*)noexcept;template<classT,class...Types>constexpradd_pointer_t<T>get_if(variant<Types...>*)noexcept;template<classT,class...Types>constexpradd_pointer_t<constT>get_if(constvariant<Types...>*)noexcept;// relational operatorstemplate<class...Types>constexprbooloperator==(constvariant<Types...>&,constvariant<Types...>&);template<class...Types>constexprbooloperator!=(constvariant<Types...>&,constvariant<Types...>&);template<class...Types>constexprbooloperator<(constvariant<Types...>&,constvariant<Types...>&);template<class...Types>constexprbooloperator>(constvariant<Types...>&,constvariant<Types...>&);template<class...Types>constexprbooloperator<=(constvariant<Types...>&,constvariant<Types...>&);template<class...Types>constexprbooloperator>=(constvariant<Types...>&,constvariant<Types...>&);template<class...Types>requires(three_way_comparable<Types>&&...)constexprcommon_comparison_category_t<compare_three_way_result_t<Types>...>operator<=>(constvariant<Types...>&,constvariant<Types...>&);// visitationtemplate<classVisitor,class...Variants>constexpr/* see description */visit(Visitor&&,Variants&&...);template<classR,classVisitor,class...Variants>constexprRvisit(Visitor&&,Variants&&...);// class monostatestructmonostate;// monostate relational operatorsconstexprbooloperator==(monostate,monostate)noexcept;constexprstrong_orderingoperator<=>(monostate,monostate)noexcept;// specialized algorithmstemplate<class...Types>constexprvoidswap(variant<Types...>&,variant<Types...>&)noexcept(/* see description */);// class bad_variant_accessclassbad_variant_access;// hash supporttemplate<classT>structhash;template<class...Types>structhash<variant<Types...>>;template<>structhash<monostate>;}// deprecatednamespacestd{template<classT>structvariant_size<volatileT>;template<classT>structvariant_size<constvolatileT>;template<size_tI,classT>structvariant_alternative<I,volatileT>;template<size_tI,classT>structvariant_alternative<I,constvolatileT>;}Class template
namespacestd{template<class...Types>classvariant{public:// constructorsconstexprvariant()noexcept(/* see description */);constexprvariant(constvariant&);constexprvariant(variant&&)noexcept(/* see description */);template<classT>constexprvariant(T&&)noexcept(/* see description */);template<classT,class...Args>constexprexplicitvariant(in_place_type_t<T>,Args&&...);template<classT,classU,class...Args>constexprexplicitvariant(in_place_type_t<T>,initializer_list<U>,Args&&...);template<size_tI,class...Args>constexprexplicitvariant(in_place_index_t<I>,Args&&...);template<size_tI,classU,class...Args>constexprexplicitvariant(in_place_index_t<I>,initializer_list<U>,Args&&...);// destructorconstexpr~variant();// assignmentconstexprvariant&operator=(constvariant&);constexprvariant&operator=(variant&&)noexcept(/* see description */);template<classT>constexprvariant&operator=(T&&)noexcept(/* see description */);// modifierstemplate<classT,class...Args>constexprT&emplace(Args&&...);template<classT,classU,class...Args>constexprT&emplace(initializer_list<U>,Args&&...);template<size_tI,class...Args>constexprvariant_alternative_t<I,variant<Types...>>&emplace(Args&&...);template<size_tI,classU,class...Args>constexprvariant_alternative_t<I,variant<Types...>>&emplace(initializer_list<U>,Args&&...);// value statusconstexprboolvalueless_by_exception()constnoexcept;constexprsize_tindex()constnoexcept;// swapconstexprvoidswap(variant&)noexcept(/* see description */);};}Class
structmonostate{};Class
classbad_variant_access:publicexception{public:// see [exception] for the specification of the special member functionsconstchar*what()constnoexceptoverride;};