variant
A type safe generalized union type
Public Types | Public Member Functions | Friends | List of all members
variant< Types > Class Template Reference

A type safe generalized union. More...

#include <variant.hpp>

Public Types

typedef variant_storage< Types... > storage_t
 The underlying variant_storage type.
 

Public Member Functions

 variant () noexcept(std::is_nothrow_constructible< storage_t, std::integral_constant< std::size_t, 0 > >::value)
 Default construct this variant using the first type in Types. More...
 
 variant (const variant &other) noexcept(detail::and_< std::is_nothrow_copy_constructible< Types >... >::value)
 Copy constructor, invokes the copy constructor of the type currently bound on other. More...
 
 variant (variant &&other) noexcept(detail::and_< std::is_nothrow_move_constructible< Types >... >::value)
 Move constructor, invokes the move constructor of the type currently bound on other. More...
 
template<class... OtherTypes>
 variant (const variant< OtherTypes...> &other) noexcept(detail::and_< std::is_nothrow_copy_constructible< OtherTypes >... >::value)
 Copy from a compatible variant. More...
 
template<class... OtherTypes>
 variant (variant< OtherTypes...> &&other) noexcept(detail::and_< std::is_nothrow_move_constructible< OtherTypes >... >::value)
 Move from a compatible variant. More...
 
template<class T >
 variant (T &&value) noexcept(std::is_nothrow_constructible< storage_t, deduce_overload_t< T && >, T && >::value)
 Generic constructor, tries to deduce the desired type. More...
 
template<class T >
 variant (std::initializer_list< T > init_list) noexcept(std::is_nothrow_constructible< storage_t, deduce_overload_t< std::initializer_list< T > >, std::initializer_list< T > >::value)
 Initializer list constructor, tries to deduce the desired type. More...
 
 ~variant () noexcept(detail::and_< std::is_nothrow_destructible< Types >... >::value)
 Destructor, calls the destructor on the currently bound type. More...
 
variantoperator= (const variant &other) noexcept(detail::and_< std::is_nothrow_copy_assignable< Types >..., std::is_nothrow_copy_constructible< Types >... >::value)
 Copy assignment operator. More...
 
variantoperator= (variant &&other) noexcept(detail::and_< std::is_nothrow_move_assignable< Types >... >::value)
 Move assignment operator. More...
 
std::size_t which () const noexcept
 Return the index of the current bound object.
 
const std::type_info & type () const noexcept
 Return the type_info of the current bound object.
 

Friends

template<class T , class Variant , class = typename std::enable_if< std::is_same< variant, typename std::decay< Variant >::type >::value >::type>
variant_element_t< variant_index< T, Variant >::value, Variant > get (Variant &&v)
 Return a reference to the current bound object. More...
 

Detailed Description

template<class... Types>
class variant< Types >

A type safe generalized union.

This class stores exactly one object of one of the type in Types. Like boost::variant , this class provides the never empty guarantee.

Difference between this class and a href = http://www.boost.org/doc/libs/1_57_0>boost::variant :

  1. Implementation of copy assignment (see variant::operator=)
  2. This variant is never LessThanComparable, even if all the Types are. We believe that comparison on variant might be confusing, and the user is still allowed to define its own comparison operators anyway
Template Parameters
TypesA list of types that can be stored in this variant.
Precondition
Types are all different.
Types can not be references
Types are not cv qualified

Constructor & Destructor Documentation

template<class... Types>
variant< Types >::variant ( )
inlinenoexcept

Default construct this variant using the first type in Types.

Only available if the first type in the list of types is DefaultConstructible.

template<class... Types>
variant< Types >::variant ( const variant< Types > &  other)
inlinenoexcept

Copy constructor, invokes the copy constructor of the type currently bound on other.

Precondition
All Types must be copy constructible.

Copy construct the value currently bound on other on this variant.

template<class... Types>
variant< Types >::variant ( variant< Types > &&  other)
inlinenoexcept

Move constructor, invokes the move constructor of the type currently bound on other.

Precondition
All Types must be MoveConstructible
template<class... Types>
template<class... OtherTypes>
variant< Types >::variant ( const variant< OtherTypes...> &  other)
inlinenoexcept

Copy from a compatible variant.

Precondition
OtherTypes Must be a subset of Types
template<class... Types>
template<class... OtherTypes>
variant< Types >::variant ( variant< OtherTypes...> &&  other)
inlinenoexcept

Move from a compatible variant.

Precondition
OtherTypes Must be a subset of Types
template<class... Types>
template<class T >
variant< Types >::variant ( T &&  value)
inlinenoexcept

Generic constructor, tries to deduce the desired type.

Use the same rules as overload resolution to determine which type in Types shall be initialized, then perfect forward the argument to the constructor of that type.

template<class... Types>
template<class T >
variant< Types >::variant ( std::initializer_list< T >  init_list)
inlinenoexcept

Initializer list constructor, tries to deduce the desired type.

Use the same rules as overload resolution to determine which type in Types shall be initialized, then perfect forward the arguments to the constructor of that type.

template<class... Types>
variant< Types >::~variant ( )
inlinenoexcept

Destructor, calls the destructor on the currently bound type.

Precondition
All Types must be Destructible

Member Function Documentation

template<class... Types>
variant& variant< Types >::operator= ( const variant< Types > &  other)
inlinenoexcept

Copy assignment operator.

Precondition
All Types must be CopyAssignable.
All Types must be NothrowDestructible.
Either all Types are NothrowCopyConstructible or all Types are NothrowMoveConstructible

If which() == other.which() calls the copy assignemnt on the bound type.

Otherwise, if the type currently bound on other is NothrowCopyConstructible destroy the current bound object and reinitialize the variant using a copy of the object currently bound on other.

If the type currently bound on other is not NothrowCopyConstructible, performs a temporary copy of other, then destroy the current bound object and move the temporary copy into this.

template<class... Types>
variant& variant< Types >::operator= ( variant< Types > &&  other)
inlinenoexcept

Move assignment operator.

Precondition
All Types must be MoveAssignable.
All Types must be NothrowDestructible.
All Types are NothrowMoveConstructible

If which() == other.which() calls the move assignemnt on the bound type.

Otherwise, destroy the current bound object and reinitialize the variant moving the object currently bound on other.

Friends And Related Function Documentation

template<class... Types>
template<class T , class Variant , class = typename std::enable_if< std::is_same< variant, typename std::decay< Variant >::type >::value >::type>
variant_element_t< variant_index<T, Variant>::value, Variant > get ( Variant &&  v)
friend

Return a reference to the current bound object.

Template Parameters
TThe expected type of the object.
Precondition
which() == variant_index <T, variant>::value
Exceptions
bad_variant_accessIf the precondition is not met.

The documentation for this class was generated from the following file: