Class template variantboost::variant — Safe, generic, stackbased discriminated union container. Synopsistemplate<typename T1, typename T2 = unspecified, ..., typename TN = unspecified> class variant { public: // types typedef unspecified types; // construct/copy/destruct variant(); variant(const variant &); template<typename T> variant(T &); template<typename T> variant(const T &); template<typename U1, typename U2, ..., typename UN> variant(variant<U1, U2, ..., UN> &); template<typename U1, typename U2, ..., typename UN> variant(const variant<U1, U2, ..., UN> &); ~variant(); // modifiers void swap(variant &); variant & operator=(const variant &); template<typename T> variant & operator=(const T &); // queries int which() const; bool empty() const; const std::type_info & type() const; // relational bool operator==(const variant &) const; template<typename U> void operator==(const U &) const; bool operator<(const variant &) const; template<typename U> void operator<(const U &) const; }; DescriptionThe Notes:

Requires: 
The first bounded type of the variant (i.e.,
T1 ) must fulfill the requirements of the
DefaultConstructible [20.1.4]
concept. 
Postconditions: 
Content of *this is the default value of the
first bounded type (i.e, T1 ). 
Throws: 
May fail with any exceptions arising from the default
constructor of T1 . 
variant(const variant & other);
Postconditions: 
Content of *this is a copy of the content of
other . 
Throws: 
May fail with any exceptions arising from the
copy constructor of other 's contained type. 
template<typename T> variant(T & operand);
Requires: 
T must be unambiguously convertible to one of
the bounded types (i.e., T1 , T2 ,
etc.). 
Postconditions: 
Content of *this is the best conversion of
operand to one of the bounded types, as determined
by standard overload resolution rules. 
Throws: 
May fail with any exceptions arising from the conversion of
operand to one of the bounded types. 
template<typename T> variant(const T & operand);
Notes: 
Same semantics as previous constructor, but allows construction from temporaries. 
template<typename U1, typename U2, ..., typename UN> variant(variant<U1, U2, ..., UN> & operand);
Requires: 
Every one of U1 ,
U2 , ..., UN must have an unambiguous
conversion to one of the bounded types (i.e., T1 ,
T2 , ..., TN ). 
Postconditions: 
If variant<U1, U2, ..., UN> is itself
one of the bounded types, then content of *this is a
copy of operand . Otherwise, content of
*this is the best conversion of the content of
operand to one of the bounded types, as determined
by standard overload resolution rules. 
Throws: 
If variant<U1, U2, ..., UN> is itself
one of the bounded types, then may fail with any exceptions arising
from the copy constructor of
variant<U1, U2, ..., UN> . Otherwise, may fail
with any exceptions arising from the conversion of the content of
operand to one of the bounded types. 
template<typename U1, typename U2, ..., typename UN> variant(const variant<U1, U2, ..., UN> & operand);
Notes: 
Same semantics as previous constructor, but allows construction from temporaries. 
~variant();
Effects: 
Destroys the content of *this . 
Throws: 
Will not throw. 
variant
modifiersvoid swap(variant & other);
Requires: 
Every bounded type must fulfill the requirements of the Assignable concept. 
Effects: 
Interchanges the content of *this and
other . 
Throws: 
If the contained type of other is the same as
the contained type of *this , then may fail with any
exceptions arising from the swap of the contents of
*this and other . Otherwise, may fail
with any exceptions arising from either of the copy constructors
of the contained types. Also, in the event of insufficient
memory, may fail with std::bad_alloc
(why?). 
variant & operator=(const variant & rhs);
Requires: 
Every bounded type must fulfill the requirements of the Assignable concept. 
Effects: 
If the contained type of rhs is the same as
the contained type of *this , then assigns the
content of rhs into the content of
*this . Otherwise, makes the content of
*this a copy of the content of rhs ,
destroying the previous content of *this . 
Throws: 
If the contained type of rhs is the same as
the contained type of *this , then may fail with any
exceptions arising from the assignment of the content of
rhs into the content *this . Otherwise,
may fail with any exceptions arising from the copy constructor
of the contained type of rhs . Also, in the event of
insufficient memory, may fail with std::bad_alloc
(why?). 
template<typename T> variant & operator=(const T & rhs);
Requires: 

Effects: 
If the contained type of *this is
T , then assigns rhs into the content
of *this . Otherwise, makes the content of
*this the best conversion of rhs to
one of the bounded types, as determined by standard overload
resolution rules, destroying the previous content of
*this . 
Throws: 
If the contained type of *this is
T , then may fail with any exceptions arising from
the assignment of rhs into the content
*this . Otherwise, may fail with any exceptions
arising from the conversion of rhs to one of the
bounded types. Also, in the event of insufficient memory, may
fail with std::bad_alloc
(why?). 
variant
queriesint which() const;
Returns: 
The zerobased index into the set of bounded types
of the contained type of *this . (For instance, if
called on a variant<int, std::string> object
containing a std::string , which()
would return 1 .) 
Throws: 
Will not throw. 
bool empty() const;
Returns: 
false : variant always contains
exactly one of its bounded types. (See
the section called “"NeverEmpty" Guarantee”
for more information.) 
Rationale: 
Facilitates generic compatibility with boost::any. 
Throws: 
Will not throw. 
const std::type_info & type() const;
Returns: 
typeid(x) , where x is the the
content of *this . 
Throws: 
Will not throw. 
variant
relationalbool operator==(const variant & rhs) const; template<typename U> void operator==(const U &) const;
Notes: 
The overload returning void exists only to
prohibit implicit conversion of the operator's righthand side
to variant ; thus, its use will (purposefully)
result in a compiletime error. 
Requires: 
Every bounded type of the variant must
fulfill the requirements of the
EqualityComparable
concept. 
Returns: 
true iff which() == rhs.which()
and
content_this == content_rhs , where
content_this is the content of *this
and content_rhs is the content of
rhs . 
Throws: 
If which() == rhs.which() then may fail with
any exceptions arising from operator==(T,T) , where
T is the contained type of
*this . 
bool operator<(const variant & rhs) const; template<typename U> void operator<(const U &) const;
Notes: 
The overload returning void exists only to
prohibit implicit conversion of the operator's righthand side
to variant ; thus, its use will (purposefully)
result in a compiletime error. 
Requires: 
Every bounded type of the variant must
fulfill the requirements of the
LessThanComparable
concept. 
Returns: 
If which() == rhs.which() then:
content_this < content_rhs , where
content_this is the content of *this
and content_rhs is the content of rhs .
Otherwise: which() < rhs.which() . 
Throws: 
If which() == rhs.which() then may fail with
any exceptions arising from operator<(T,T) ,
where T is the contained type of
*this . 