Backport
bpstd::variant< Types > Class Template Reference

The class template variant represents a type-safe union. More...

#include <variant.hpp>

Public Member Functions

constexpr variant (detail::enable_overload_if_t< is_default_constructible, variant_ctor >={}) noexcept(std::is_nothrow_default_constructible< first_type >::value)
 Default constructor. More...
 
 variant (detail::enable_overload_if_t< is_copy_constructible, const variant & > other) noexcept(bpstd::conjunction< bpstd::is_nothrow_copy_constructible< Types >... >::value)
 Copy constructor. More...
 
 variant (detail::disable_overload_if_t< is_copy_constructible, const variant & > other)=delete
 
 variant (detail::enable_overload_if_t< is_move_constructible, variant && > other) noexcept(bpstd::conjunction< bpstd::is_nothrow_move_constructible< Types >... >::value)
 Move constructor. More...
 
 variant (detail::disable_overload_if_t< is_move_constructible, variant && > other)=delete
 
template<typename T , typename = enable_if_convertible<T>>
constexpr variant (T &&t) noexcept(bpstd::is_nothrow_constructible< typename bpstd::variant< Types... >::template T_j< T >, T >::value)
 Converting constructor. More...
 
template<typename T , typename... Args, typename = enable_if_t<bpstd::is_constructible<T,Args...>::value>>
constexpr variant (in_place_type_t< T >, Args &&... args)
 Constructs a variant with the specified alternative T and initializes the contained value with the arguments std::forward<Args>(args).... More...
 
template<typename T , typename U , typename... Args, typename = enable_if_t<bpstd::is_constructible<T,std::initializer_list<U>,Args...>::value>>
constexpr variant (in_place_type_t< T >, std::initializer_list< U > il, Args &&... args)
 Constructs a variant with the specified alternative T and initializes the contained value with the arguments il, std::forward<Args>(args)..... More...
 
template<std::size_t I, typename... Args, typename = enable_if_t<i_is_in_range<I>::value && std::is_constructible<variant_alternative_t<I,variant>,Args...>::value>>
constexpr variant (in_place_index_t< I >, Args &&... args)
 Constructs a variant with the alternative T_i specified by the index I and initializes the contained value with the arguments std::forward<Args>(args).... More...
 
template<std::size_t I, typename U , typename... Args, typename = enable_if_t<i_is_in_range<I>::value && std::is_constructible<variant_alternative_t<I,variant>,std::initializer_list<U>,Args...>::value>>
constexpr variant (in_place_index_t< I >, std::initializer_list< U > il, Args &&... args)
 Constructs a variant with the alternative T_i specified by the index I and initializes the contained value with the arguments il, std::forward<Args>(args).... More...
 
variant & operator= (detail::enable_overload_if_t< is_copy_assignable, const variant & > other)
 Copy assigns the contents of other to this. More...
 
variant & operator= (detail::disable_overload_if_t< is_copy_assignable, const variant & > other)=delete
 
variant & operator= (detail::enable_overload_if_t< is_move_assignable, variant && > other) noexcept(conjunction< std::is_nothrow_move_constructible< Types >..., std::is_nothrow_move_assignable< Types >... >::value)
 Move assigns the contents of other to this. More...
 
variant & operator= (detail::disable_overload_if_t< is_move_assignable, variant && > other)=delete
 
template<typename T , typename = enable_if_convert_assignable<T>>
variant & operator= (T &&t) noexcept(std::is_nothrow_assignable< typename bpstd::variant< Types... >::template T_j< T >, T >::value &&std::is_nothrow_constructible< typename bpstd::variant< Types... >::template T_j< T >, T >::value)
 
constexpr std::size_t index () const noexcept
 Returns the zero-based index of the alternative that is currently held by the variant. More...
 
constexpr bool valueless_by_exception () const noexcept
 Returns false if and only if the variant holds a value. More...
 
template<typename T , typename... Args, typename = enable_if_t<std::is_constructible<T,Args...>::value>>
T & emplace (Args &&... args)
 Creates a new value in-place. More...
 
template<typename T , typename U , typename... Args, typename = enable_if_t<std::is_constructible<T,std::initializer_list<U>&,Args...>::value>>
T & emplace (std::initializer_list< U > il, Args &&... args)
 Creates a new value in-place. More...
 
template<std::size_t I, typename... Args, typename = enable_if_t<std::is_constructible<detail::nth_type_t<I,Types...>,Args...>::value>>
variant_alternative_t< I, variant > & emplace (Args &&... args)
 Creates a new value in-place. More...
 
template<std::size_t I, typename U , typename... Args, typename = enable_if_t<std::is_constructible<detail::nth_type_t<I,Types...>,std::initializer_list<U>&,Args...>::value>>
variant_alternative_t< I, variant > & emplace (std::initializer_list< U > il, Args &&... args)
 Creates a new value in-place. More...
 
void swap (variant &rhs) noexcept(bpstd::conjunction< bpstd::is_nothrow_move_constructible< Types >..., bpstd::is_nothrow_swappable< Types >... >::value)
 Swaps the contents of this and rhs. More...
 
template<typename T , typename >
constexpr BPSTD_INLINE_VISIBILITY variant (T &&t) noexcept(bpstd::is_nothrow_constructible< typename bpstd::variant< Types... >::template T_j< T >, T >::value)
 
template<typename T , typename... Args, typename >
constexpr BPSTD_INLINE_VISIBILITY variant (in_place_type_t< T >, Args &&... args)
 
template<typename T , typename U , typename... Args, typename >
constexpr BPSTD_INLINE_VISIBILITY variant (in_place_type_t< T >, std::initializer_list< U > il, Args &&... args)
 
template<std::size_t I, typename... Args, typename >
constexpr BPSTD_INLINE_VISIBILITY variant (in_place_index_t< I >, Args &&... args)
 
template<std::size_t I, typename U , typename... Args, typename >
constexpr BPSTD_INLINE_VISIBILITY variant (in_place_index_t< I >, std::initializer_list< U > il, Args &&... args)
 
template<typename T , typename >
BPSTD_INLINE_VISIBILITY bpstd::variant< Types... > & operator= (T &&t) noexcept(std::is_nothrow_assignable< typename bpstd::variant< Types... >::template T_j< T >, T >::value &&std::is_nothrow_constructible< typename bpstd::variant< Types... >::template T_j< T >, T >::value)
 
template<typename T , typename... Args, typename >
BPSTD_INLINE_VISIBILITY T & emplace (Args &&...args)
 
template<typename T , typename U , typename... Args, typename >
BPSTD_INLINE_VISIBILITY T & emplace (std::initializer_list< U > il, Args &&...args)
 
template<std::size_t I, typename... Args, typename >
BPSTD_INLINE_VISIBILITY bpstd::variant_alternative_t< I, bpstd::variant< Types... > > & emplace (Args &&...args)
 
template<std::size_t I, typename U , typename... Args, typename >
BPSTD_INLINE_VISIBILITY bpstd::variant_alternative_t< I, bpstd::variant< Types... > > & emplace (std::initializer_list< U > il, Args &&... args)
 

Friends

template<typename... UTypes>
BPSTD_CPP14_CONSTEXPR bool operator== (const variant< UTypes... > &, const variant< UTypes... > &) noexcept
 
template<typename... UTypes>
BPSTD_CPP14_CONSTEXPR bool operator!= (const variant< UTypes... > &, const variant< UTypes... > &) noexcept
 
template<typename... UTypes>
BPSTD_CPP14_CONSTEXPR bool operator< (const variant< UTypes... > &, const variant< UTypes... > &) noexcept
 
template<typename... UTypes>
BPSTD_CPP14_CONSTEXPR bool operator> (const variant< UTypes... > &, const variant< UTypes... > &) noexcept
 
template<typename... UTypes>
BPSTD_CPP14_CONSTEXPR bool operator<= (const variant< UTypes... > &, const variant< UTypes... > &) noexcept
 
template<typename... UTypes>
BPSTD_CPP14_CONSTEXPR bool operator>= (const variant< UTypes... > &, const variant< UTypes... > &) noexcept
 
template<typename Visitor , typename Variant >
BPSTD_CPP14_CONSTEXPR friend detail::variant_visitor_invoke_result_t< Visitor, Variant > visit (Visitor &&, Variant &&)
 Visits the variant v with the given visitor. More...
 
template<std::size_t I, typename... UTypes>
BPSTD_CPP14_CONSTEXPR variant_alternative_t< I, variant< UTypes... > > & get (variant< UTypes... > &)
 
template<std::size_t I, typename... UTypes>
BPSTD_CPP14_CONSTEXPR variant_alternative_t< I, variant< UTypes... > > && get (variant< UTypes... > &&)
 
template<std::size_t I, typename... UTypes>
const BPSTD_CPP14_CONSTEXPR variant_alternative_t< I, variant< UTypes... > > & get (const variant< UTypes... > &)
 
template<std::size_t I, typename... UTypes>
const BPSTD_CPP14_CONSTEXPR variant_alternative_t< I, variant< UTypes... > > && get (const variant< UTypes... > &&)
 
template<std::size_t I, typename... UTypes>
constexpr friend add_pointer_t< variant_alternative_t< I, variant< UTypes... > > > get_if (variant< UTypes... > *) noexcept
 
template<std::size_t I, typename... UTypes>
constexpr friend add_pointer_t< const variant_alternative_t< I, variant< UTypes... > > > get_if (const variant< UTypes... > *) noexcept
 

Detailed Description

template<typename... Types>
class bpstd::variant< Types >

The class template variant represents a type-safe union.

An instance of variant at any given time either holds a value of one of its alternative types, or it holds no value (this state is hard to achieve,

See also
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 (variant<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.

As with unions, the default-initialized variant holds a value of its first alternative, unless that alternative is not default-constructible (in which case default constructor won't compile: the helper class monostate can be used to make such variants default-constructible)

Template Parameters
Typesthe types that may be stored in this variant. All types must be (possibly cv-qualified) non-array object types

Definition at line 76 of file variant.hpp.

Constructor & Destructor Documentation

◆ variant() [1/8]

template<typename... Types>
constexpr BPSTD_INLINE_VISIBILITY bpstd::variant< Types >::variant ( detail::enable_overload_if_t< is_default_constructible, variant_ctor >  = {})
inlineconstexprnoexcept

Default constructor.

Constructs a variant holding the value-initialized value of the first alternative (index() is zero).

Note
This constructor is constexpr if and only if the value initialization of the alternative type T_0 would satisfy the requirements for a constexpr function.
This overload only participates in overload resolution if std::is_default_constructible_v<T_0> is true.

Definition at line 1094 of file variant.hpp.

◆ variant() [2/8]

template<typename... Types>
BPSTD_INLINE_VISIBILITY bpstd::variant< Types >::variant ( detail::enable_overload_if_t< is_copy_constructible, const variant< Types > & >  other)
inlinenoexcept

Copy constructor.

If other is not valueless_by_exception, constructs a variant holding the same alternative as other and direct-initializes the contained value with std::get<other.index()>(other). Otherwise, initializes a valueless_by_exception variant.

Note
This overload only participates in overload resolution if std::is_copy_constructible_v<T_i> is true for all T_i in Types....
Parameters
otherthe other variant to copy

Definition at line 1103 of file variant.hpp.

◆ variant() [3/8]

template<typename... Types>
BPSTD_INLINE_VISIBILITY bpstd::variant< Types >::variant ( detail::enable_overload_if_t< is_move_constructible, variant< Types > && >  other)
inlinenoexcept

Move constructor.

If other is not valueless_by_exception, constructs a variant holding the same alternative as other and direct-initializes the contained value with std::get<other.index()>(std::move(other)). Otherwise, initializes a valueless_by_exception variant.

Note
This overload only participates in overload resolution if std::is_move_constructible_v<T_i> is true for all T_i in Types...
Parameters
otherthe other variant to move

Definition at line 1121 of file variant.hpp.

◆ variant() [4/8]

template<typename... Types>
template<typename T , typename = enable_if_convertible<T>>
constexpr bpstd::variant< Types >::variant ( T &&  t) const
constexprnoexcept

Converting constructor.

Constructs a variant holding the alternative type T_j that would be selected by overload resolution for the expression F(std::forward<T>(t)) if there was an overload of imaginary function F(T_i) for every T_i from Types... in scope at the same time.

Direct-initializes the contained value as if by direct non-list-initialization from std::forward<T>(t).

Note
This overload only participates in overload resolution if
  • sizeof...(Types) > 0
  • std::is_same_v<decay_t<T>, variant> is false
  • decay_t<T> is neither a specialization of in_place_type_t nor a specialization of in_place_index_t
  • std::is_constructible_v<T_j, T> is true
  • and the expression F(std::forward<T>(t)) (with F being the above-mentioned set of imaginary functions) is well formed.
This constructor is a constexpr constructor if T_j's selected constructor is a constexpr constructor.
Parameters
tthe value to direct-initialize

◆ variant() [5/8]

template<typename... Types>
template<typename T , typename... Args, typename = enable_if_t<bpstd::is_constructible<T,Args...>::value>>
constexpr bpstd::variant< Types >::variant ( in_place_type_t< T >  ,
Args &&...  args 
)
explicitconstexpr

Constructs a variant with the specified alternative T and initializes the contained value with the arguments std::forward<Args>(args)....

Note
If T's selected constructor is a constexpr constructor, this constructor is also a constexpr constructor.
This overload only participates in overload resolution if there is exactly one occurrence of T in Types... and std::is_constructible_v<T, Args...> is true
Parameters
argsthe arguments to forward to T's constructor

◆ variant() [6/8]

template<typename... Types>
template<typename T , typename U , typename... Args, typename = enable_if_t<bpstd::is_constructible<T,std::initializer_list<U>,Args...>::value>>
constexpr bpstd::variant< Types >::variant ( in_place_type_t< T >  ,
std::initializer_list< U >  il,
Args &&...  args 
)
explicitconstexpr

Constructs a variant with the specified alternative T and initializes the contained value with the arguments il, std::forward<Args>(args).....

Note
If T's selected constructor is a constexpr constructor, this constructor is also a constexpr constructor.
This overload only participates in overload resolution if there is exactly one occurrence of T in Types... and std::is_constructible_v<T, initializer_list&, Args...> is true.
Parameters
ilinitializer list of type U
argsthe arguments to forward to T's constructor

◆ variant() [7/8]

template<typename... Types>
template<std::size_t I, typename... Args, typename = enable_if_t<i_is_in_range<I>::value && std::is_constructible<variant_alternative_t<I,variant>,Args...>::value>>
constexpr bpstd::variant< Types >::variant ( in_place_index_t< I >  ,
Args &&...  args 
)
explicitconstexpr

Constructs a variant with the alternative T_i specified by the index I and initializes the contained value with the arguments std::forward<Args>(args)....

Note
If T_i's selected constructor is a constexpr constructor, this constructor is also a constexpr constructor.
This overload only participates in overload resolution if I < sizeof...(Types) and std::is_constructible_v<T_i, Args...> is true
Parameters
argsthe arguments to forward to T_i's constructor

◆ variant() [8/8]

template<typename... Types>
template<std::size_t I, typename U , typename... Args, typename = enable_if_t<i_is_in_range<I>::value && std::is_constructible<variant_alternative_t<I,variant>,std::initializer_list<U>,Args...>::value>>
constexpr bpstd::variant< Types >::variant ( in_place_index_t< I >  ,
std::initializer_list< U >  il,
Args &&...  args 
)
explicitconstexpr

Constructs a variant with the alternative T_i specified by the index I and initializes the contained value with the arguments il, std::forward<Args>(args)....

Note
If T_i's selected constructor is a constexpr constructor, this constructor is also a constexpr constructor.
This overload only participates in overload resolution if I < sizeof...(Types) and std::is_constructible_v<T_i, std::initializer_list<U>&, Args...> is true.
Parameters
ilinitializer list of type U
argsthe arguments to forward to T_i 's constructor

Member Function Documentation

◆ emplace() [1/4]

template<typename... Types>
template<typename T , typename... Args, typename = enable_if_t<std::is_constructible<T,Args...>::value>>
T& bpstd::variant< Types >::emplace ( Args &&...  args)

Creates a new value in-place.

Equivalent to emplace<I>(std::forward<Args>(args)...), where I is the zero-based index of T in Types....

Note
This overload only participates in overload resolution if std::is_constructible_v<T, Args...> is true, and T occurs exactly once in Types...
Template Parameters
Tthe type to construct
Parameters
argsthe arguments to forward to the constructor of T
Returns
reference to constructed element

◆ emplace() [2/4]

template<typename... Types>
template<std::size_t I, typename... Args, typename = enable_if_t<std::is_constructible<detail::nth_type_t<I,Types...>,Args...>::value>>
variant_alternative_t<I, variant>& bpstd::variant< Types >::emplace ( Args &&...  args)

Creates a new value in-place.

First, destroys the currently contained value (if any). Then direct-initializes the contained value as if constructing a value of type T_I with the arguments std::forward<Args>(args)...

If an exception is thrown, *this may become valueless_by_exception.

Note
This overload only participates in overload resolution if std::is_constructible_v<T_I, Args...> is true.

The behavior is undefined if I is not less than sizeof...(Types).

Template Parameters
Ithe index of the variant alternative to construct
Parameters
argsthe arguments to forward to the constructor of T
Returns
reference to constructed element

◆ emplace() [3/4]

template<typename... Types>
template<typename T , typename U , typename... Args, typename = enable_if_t<std::is_constructible<T,std::initializer_list<U>&,Args...>::value>>
T& bpstd::variant< Types >::emplace ( std::initializer_list< U >  il,
Args &&...  args 
)

Creates a new value in-place.

Equivalent to emplace<I>(il, std::forward<Args>(args)...), where I is the zero-based index of T in Types....

Note
This overload only participates in overload resolution if std::is_constructible_v<T, std::initializer_list<U>, Args...> is true, and T occurs exactly once in Types...
Template Parameters
Tthe type to construct
Parameters
ilan initializer list of entries
argsthe arguments to forward to the constructor of T
Returns
reference to constructed element

◆ emplace() [4/4]

template<typename... Types>
template<std::size_t I, typename U , typename... Args, typename = enable_if_t<std::is_constructible<detail::nth_type_t<I,Types...>,std::initializer_list<U>&,Args...>::value>>
variant_alternative_t<I, variant>& bpstd::variant< Types >::emplace ( std::initializer_list< U >  il,
Args &&...  args 
)

Creates a new value in-place.

First, destroys the currently contained value (if any). Then direct-initializes the contained value as if constructing a value of type T_I with the arguments il,std::forward<Args>(args)...

If an exception is thrown, *this may become valueless_by_exception.

Note
This overload only participates in overload resolution if std::is_constructible_v<T_I, std::initializer_list<U> Args...> is true.

The behavior is undefined if I is not less than sizeof...(Types).

Template Parameters
Ithe index of the variant alternative to construct
Parameters
argsthe arguments to forward to the constructor of T
Returns
reference to constructed element

◆ index()

template<typename... Types>
constexpr BPSTD_INLINE_VISIBILITY std::size_t bpstd::variant< Types >::index
inlineconstexprnoexcept

Returns the zero-based index of the alternative that is currently held by the variant.

If the variant is valueless_by_exception, returns variant_npos.

Returns
the zero-based index

Definition at line 1311 of file variant.hpp.

◆ operator=() [1/2]

template<typename... Types>
BPSTD_INLINE_VISIBILITY bpstd::variant< Types... > & bpstd::variant< Types >::operator= ( detail::enable_overload_if_t< is_copy_assignable, const variant< Types > & >  other)
inline

Copy assigns the contents of other to this.

If the active variant alternative is the same as other , then this will perform an assignment. Otherwise, this destructs the currently active alternative and performs a copy construction.

Parameters
otherthe other variant to copy

Definition at line 1203 of file variant.hpp.

◆ operator=() [2/2]

template<typename... Types>
BPSTD_INLINE_VISIBILITY bpstd::variant< Types... > & bpstd::variant< Types >::operator= ( detail::enable_overload_if_t< is_move_assignable, variant< Types > && >  other)
inlinenoexcept

Move assigns the contents of other to this.

If the active variant alternative is the same as other , then this will perform an assignment. Otherwise, this destructs the currently active alternative and performs a move construction.

Parameters
otherthe other variant to move

Definition at line 1242 of file variant.hpp.

◆ swap()

template<typename... Types>
BPSTD_INLINE_VISIBILITY void bpstd::variant< Types >::swap ( variant< Types > &  rhs)
inlinenoexcept

Swaps the contents of this and rhs.

Parameters
rhsthe entry to swap with

Definition at line 1400 of file variant.hpp.

◆ valueless_by_exception()

template<typename... Types>
constexpr BPSTD_INLINE_VISIBILITY bool bpstd::variant< Types >::valueless_by_exception
inlineconstexprnoexcept

Returns false if and only if the variant holds a value.

Returns
false if and only if the variant holds a value

Definition at line 1319 of file variant.hpp.

Friends And Related Function Documentation

◆ visit

template<typename... Types>
template<typename Visitor , typename Variant >
BPSTD_CPP14_CONSTEXPR friend detail::variant_visitor_invoke_result_t<Visitor,Variant> visit ( Visitor &&  ,
Variant &&   
)
friend

Visits the variant v with the given visitor.

Parameters
visitorthe visitor to visit the active entry of v
vthe variant to visit
Returns
the result of visiting the variant v

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