Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
arene::base::variant< Ts > Class Template Reference

Forward declaration of variant. More...

Inheritance diagram for arene::base::variant< Ts >:
Inheritance graph

Public Member Functions

template<typename T0 = nth_type<0>, constraints< std::enable_if_t< std::is_default_constructible< T0 >::value > > = nullptr>
 variant () noexcept(std::is_nothrow_default_constructible< T0 >::value)
 Default constructor, which initializes the variant to a default constructed instance of its 0'th alternative.
 
 variant (copy_ctor_arg const &other) noexcept(arene::base::all_of_v< std::is_nothrow_copy_constructible< Ts >::value... >)
 Copy Constructor from another variant of the same alternative types.
 
 variant (dummy_copy_ctor_source const &other)=delete
 Copy Constructor from another variant of the same alternative types.
 
 variant (dummy_move_ctor_source &&other)=delete
 Move Constructor form another variant.
 
 variant (move_ctor_arg &&other) noexcept(base::all_of_v< std::is_nothrow_move_constructible< Ts >::value... >)
 Move Constructor from another variant.
 
template<typename T, std::size_t I = variant_detail::accepted_index<variant, T>::type::value, typename Tj = nth_type<I>, constraints< std::enable_if_t<!std::is_same< typename std::decay< T >::type, variant >::value >, std::enable_if_t< std::is_constructible< Tj, T >::value > > = nullptr>
 variant (T &&value) noexcept(std::is_nothrow_constructible< Tj, T >::value)
 Constructor that initializes this variant from one of its alternative types.
 
 ~variant ()
 Calls the destructor of the alternative type.
 
template<std::size_t I, typename... Args>
auto emplace (Args &&... args) noexcept(std::is_nothrow_constructible< nth_type< I >, Args... >::value) -> nth_type< I > &
 Emplaces a value into the variant by index.
 
template<typename T, typename... Args>
auto emplace (Args &&... args) noexcept(std::is_nothrow_constructible< T, Args... >::value) -> T &
 Emplaces a value into the variant by type.
 
template<std::size_t I, typename U, typename... Args>
auto emplace (std::initializer_list< U > init, Args &&... args) noexcept(std::is_nothrow_constructible< nth_type< I >, std::initializer_list< U >, Args... >::value) -> nth_type< I > &
 Emplaces a value into the variant by index.
 
template<typename T, typename U, typename... Args>
auto emplace (std::initializer_list< U > init, Args &&... args) noexcept(std::is_nothrow_constructible< T, std::initializer_list< U >, Args... >::value) -> T &
 Emplaces a value into the variant by type.
 
template<std::size_t Index>
auto get () &&noexcept(!detail::are_exceptions_enabled_v) -> nth_type< Index > &&
 Get the object stored in this variant by the index.
 
template<typename T>
auto get () &&noexcept(!detail::are_exceptions_enabled_v) -> T &&
 Get by type.
 
template<std::size_t Index>
auto get () &noexcept(!detail::are_exceptions_enabled_v) -> nth_type< Index > &
 Get the object stored in this variant by the index.
 
template<typename T>
auto get () &noexcept(!detail::are_exceptions_enabled_v) -> T &
 Get by type.
 
template<std::size_t Index>
auto get () const &&noexcept(!detail::are_exceptions_enabled_v) -> nth_type< Index > const &&
 Get the object stored in this variant by the index.
 
template<typename T>
auto get () const &&noexcept(!detail::are_exceptions_enabled_v) -> T const &&
 Get by type.
 
template<std::size_t Index>
auto get () const &noexcept(!detail::are_exceptions_enabled_v) -> nth_type< Index > const &
 Get the object stored in this variant by the index.
 
template<typename T>
auto get () const &noexcept(!detail::are_exceptions_enabled_v) -> T const &
 Get by type.
 
auto index () const noexcept -> std::size_t
 Get the index of the alternative type that this variant currently holds.
 
auto operator= (copy_assignment_arg const &rhs) noexcept(base::all_of_v< std::is_nothrow_copy_constructible< Ts >::value... > &&base::all_of_v< std::is_nothrow_copy_assignable< Ts >::value... >) -> variant &
 Copy Assignment operator from another variant of the same alternative types.
 
auto operator= (dummy_copy_assign_source const &rhs) -> variant &=delete
 Copy Assignment operator from another variant of the same alternative types.
 
auto operator= (dummy_move_assign_source &&rhs) -> variant &=delete
 Move Assignment operator from another variant of the same alternative types.
 
auto operator= (move_assignment_arg &&rhs) noexcept(base::all_of_v< std::is_nothrow_move_constructible< Ts >::value... > &&base::all_of_v< std::is_nothrow_move_assignable< Ts >::value... >) -> variant &
 Move Assignment operator from another variant of the same alternative types.
 
constexpr auto valueless_by_exception () const noexcept -> bool
 Check if the variant is valueless.
 

Friends

auto operator< (variant const &lhs, variant const &rhs) noexcept -> bool
 Less-than comparison.
 
auto operator== (variant const &lhs, variant const &rhs) noexcept -> bool
 Equality comparison.
 

Detailed Description

template<typename... Ts>
class arene::base::variant< Ts >

Forward declaration of variant.

a backport of C++17's std::variant

Template Parameters
Tsthe types in the variant
TsThe set of alternatives that can be held by the variant.

Constructor & Destructor Documentation

◆ variant() [1/6]

template<typename... Ts>
template<typename T0 = nth_type<0>, constraints< std::enable_if_t< std::is_default_constructible< T0 >::value > > = nullptr>
arene::base::variant< Ts >::variant ( )
inlinenoexcept

Default constructor, which initializes the variant to a default constructed instance of its 0'th alternative.

Precondition
Only participates in overload resolution if the first type in Ts is default constructible.

◆ variant() [2/6]

template<typename... Ts>
arene::base::variant< Ts >::variant ( copy_ctor_arg const & other)
inlinenoexcept

Copy Constructor from another variant of the same alternative types.

If the other variant is valueless, this variant will also be valueless.

Precondition
Only participates in overload resolution if every type in Ts is copy constructible.
Parameters
otherthe other variant to copy from

◆ variant() [3/6]

template<typename... Ts>
arene::base::variant< Ts >::variant ( dummy_copy_ctor_source const & other)
delete

Copy Constructor from another variant of the same alternative types.

If the other variant is valueless, this variant will also be valueless.

Precondition
Only participates in overload resolution if every type in Ts is copy constructible.
Parameters
otherthe other variant to copy from

◆ variant() [4/6]

template<typename... Ts>
arene::base::variant< Ts >::variant ( move_ctor_arg && other)
inlinenoexcept

Move Constructor from another variant.

If the other variant is valueless, this variant will also be valueless.

Precondition
Only participates in overload resolution if every type in Ts is move constructible.
Parameters
otherthe other variant to move from

◆ variant() [5/6]

template<typename... Ts>
arene::base::variant< Ts >::variant ( dummy_move_ctor_source && other)
delete

Move Constructor form another variant.

If the other variant is valueless, this variant will also be valueless.

Precondition
Only participates in overload resolution if every type in Ts is move constructible.
Parameters
otherthe other variant to move from

◆ variant() [6/6]

template<typename... Ts>
template<typename T, std::size_t I = variant_detail::accepted_index<variant, T>::type::value, typename Tj = nth_type<I>, constraints< std::enable_if_t<!std::is_same< typename std::decay< T >::type, variant >::value >, std::enable_if_t< std::is_constructible< Tj, T >::value > > = nullptr>
arene::base::variant< Ts >::variant ( T && value)
inlinenoexcept

Constructor that initializes this variant from one of its alternative types.

This constructor will pick the constructor overload of the alternative type Tj that T_i x[] = { std::forward<T>(value) }; is valid. If more than one alternative type satisfies this condition, the compilation will fail.

Template Parameters
Tthe type to construct this variant
Ithe deduced index of the alternative type that T can construct
Tjthe deduced alternative type that T can construct
Parameters
valuethe object to forward into the variant

◆ ~variant()

template<typename... Ts>
arene::base::variant< Ts >::~variant ( )
inline

Calls the destructor of the alternative type.

Member Function Documentation

◆ emplace() [1/4]

template<typename... Ts>
template<std::size_t I, typename... Args>
auto arene::base::variant< Ts >::emplace ( Args &&... args) -> nth_type<I>&
inlinenoexcept

Emplaces a value into the variant by index.

Template Parameters
Ithe index of the alternative type to emplace
Argsthe variadic argument type to pass into the constructor of the alternative type
Parameters
argsArguments to be forwarded to the constructor of the emplaced type.
Returns
A reference to the emplaced object.
Postcondition
If the variant was not valueless, the currently held value is destroyed
The variant will hold the value the returned reference points to.
holds_alternative<I>(*this) will return true
index() will return I

◆ emplace() [2/4]

template<typename... Ts>
template<typename T, typename... Args>
auto arene::base::variant< Ts >::emplace ( Args &&... args) -> T&
inlinenoexcept

Emplaces a value into the variant by type.

Template Parameters
Tthe alternative type to emplace
Parameters
argsArguments to be forwarded to the constructor of the emplaced type.
Returns
A reference to the emplaced object.
Postcondition
If the variant was not valueless, the currently held value is destroyed
The variant will hold the value the returned reference points to.
holds_alternative<T>(*this) will return true
index() will return the the equivalent of index_of_v<T, Ts...>

◆ emplace() [3/4]

template<typename... Ts>
template<std::size_t I, typename U, typename... Args>
auto arene::base::variant< Ts >::emplace ( std::initializer_list< U > init,
Args &&... args ) -> nth_type<I>&
inlinenoexcept

Emplaces a value into the variant by index.

Template Parameters
Ithe index of the alternative type to emplace
Argsthe variadic argument type to pass into the constructor of the alternative type
Parameters
initAn initializer list to forward to the constructor of the emplaced type.
argsArguments to be forwarded to the constructor of the emplaced type.
Returns
A reference to the emplaced object.
Postcondition
If the variant was not valueless, the currently held value is destroyed
The variant will hold the value the returned reference points to.
holds_alternative<I>(*this) will return true
index() will return I

◆ emplace() [4/4]

template<typename... Ts>
template<typename T, typename U, typename... Args>
auto arene::base::variant< Ts >::emplace ( std::initializer_list< U > init,
Args &&... args ) -> T&
inlinenoexcept

Emplaces a value into the variant by type.

Template Parameters
Tthe alternative type to emplace
Parameters
initAn initializer list to forward to the constructor of the emplaced type.
argsArguments to be forwarded to the constructor of the emplaced type.
Returns
A reference to the emplaced object.
Postcondition
If the variant was not valueless, the currently held value is destroyed
The variant will hold the value the returned reference points to.
holds_alternative<T>(*this) will return true
index() will return the the equivalent of index_of_v<T, Ts...>

◆ get() [1/8]

template<typename... Ts>
template<std::size_t Index>
auto arene::base::variant< Ts >::get ( ) && -> nth_type<Index>&&
inlinenoexcept

Get the object stored in this variant by the index.

Template Parameters
Indexthe index into Ts of the alternative to get
Exceptions
bad_variant_accessif ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type index of the object currently held by the variant does not match the index requested.
Precondition
If ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type index of the object currently held by the variant must match the type requested else ARENE_PRECONDITION violation.
Returns
nth_type<Index>&& The reference to the held object.

◆ get() [2/8]

template<typename... Ts>
template<typename T>
auto arene::base::variant< Ts >::get ( ) && -> T&&
inlinenoexcept

Get by type.

Template Parameters
Tthe alternative type to get
Exceptions
bad_variant_accessif ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type of the object currently held by the variant does not match the type requested.
Precondition
If ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type of the object currently held by the variant must match the type requested else ARENE_PRECONDITION violation.
Returns
T&& The reference to the held object.

◆ get() [3/8]

template<typename... Ts>
template<std::size_t Index>
auto arene::base::variant< Ts >::get ( ) & -> nth_type<Index>&
inlinenoexcept

Get the object stored in this variant by the index.

Template Parameters
Indexthe index into Ts of the alternative to get
Exceptions
bad_variant_accessif ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type index of the object currently held by the variant does not match the index requested.
Precondition
If ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type index of the object currently held by the variant must match the type requested else ARENE_PRECONDITION violation.
Returns
nth_type<Index>& The reference to the held object.

◆ get() [4/8]

template<typename... Ts>
template<typename T>
auto arene::base::variant< Ts >::get ( ) & -> T&
inlinenoexcept

Get by type.

Template Parameters
Tthe alternative type to get
Exceptions
bad_variant_accessif ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type of the object currently held by the variant does not match the type requested.
Precondition
If ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type of the object currently held by the variant must match the type requested else ARENE_PRECONDITION violation.
Returns
T& The reference to the held object.

◆ get() [5/8]

template<typename... Ts>
template<std::size_t Index>
auto arene::base::variant< Ts >::get ( ) const && -> nth_type<Index> const&&
inlinenoexcept

Get the object stored in this variant by the index.

Template Parameters
Indexthe index into Ts of the alternative to get
Exceptions
bad_variant_accessif ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type index of the object currently held by the variant does not match the index requested.
Precondition
If ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type index of the object currently held by the variant must match the type requested else ARENE_PRECONDITION violation.
Returns
nth_type<Index> const&& The reference to the held object.

◆ get() [6/8]

template<typename... Ts>
template<typename T>
auto arene::base::variant< Ts >::get ( ) const && -> T const&&
inlinenoexcept

Get by type.

Template Parameters
Tthe alternative type to get
Exceptions
bad_variant_accessif ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type of the object currently held by the variant does not match the type requested.
Precondition
If ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type of the object currently held by the variant must match the type requested else ARENE_PRECONDITION violation.
Returns
T const&& The reference to the held object.

◆ get() [7/8]

template<typename... Ts>
template<std::size_t Index>
auto arene::base::variant< Ts >::get ( ) const & -> nth_type<Index> const&
inlinenoexcept

Get the object stored in this variant by the index.

Template Parameters
Indexthe index into Ts of the alternative to get
Exceptions
bad_variant_accessif ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type index of the object currently held by the variant does not match the index requested.
Precondition
If ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type index of the object currently held by the variant must match the type requested else ARENE_PRECONDITION violation.
Returns
nth_type<Index> const& The reference to the held object.

◆ get() [8/8]

template<typename... Ts>
template<typename T>
auto arene::base::variant< Ts >::get ( ) const & -> T const&
inlinenoexcept

Get by type.

Template Parameters
Tthe alternative type to get
Exceptions
bad_variant_accessif ARENE_IS_ON(ARENE_EXCEPTIONS_ENABLED) and if the type of the object currently held by the variant does not match the type requested.
Precondition
If ARENE_IS_OFF(ARENE_EXCEPTIONS_ENABLED) the type of the object currently held by the variant must match the type requested else ARENE_PRECONDITION violation.
Returns
T const& The reference to the held object.

◆ index()

template<typename... Ts>
auto arene::base::variant< Ts >::index ( ) const -> std::size_t
inlinenoexcept

Get the index of the alternative type that this variant currently holds.

Returns
std::size_t the index of the alternative type that this variant currently holds, or variant_npos if the variant is valueless.

◆ operator=() [1/4]

template<typename... Ts>
auto arene::base::variant< Ts >::operator= ( copy_assignment_arg const & rhs) -> variant&
inlinenoexcept

Copy Assignment operator from another variant of the same alternative types.

If the other type is valueless, or exception is thrown during copying, this variant will also be valueless.

Precondition
Only participates in overload resolution if every type in Ts is copy-assignable.
Parameters
rhsthe other variant to copy from

◆ operator=() [2/4]

template<typename... Ts>
auto arene::base::variant< Ts >::operator= ( dummy_copy_assign_source const & rhs) -> variant &=delete
delete

Copy Assignment operator from another variant of the same alternative types.

If the other type is valueless, or exception is thrown during copying, this variant will also be valueless.

Precondition
Only participates in overload resolution if every type in Ts is copy-assignable.
Parameters
rhsthe other variant to copy from

◆ operator=() [3/4]

template<typename... Ts>
auto arene::base::variant< Ts >::operator= ( dummy_move_assign_source && rhs) -> variant &=delete
delete

Move Assignment operator from another variant of the same alternative types.

If the other type is valueless, this variant will also be valueless.

Precondition
Only participates in overload resolution if every type in Ts is move-assignable.
Parameters
rhsthe other variant to move from

◆ operator=() [4/4]

template<typename... Ts>
auto arene::base::variant< Ts >::operator= ( move_assignment_arg && rhs) -> variant&
inlinenoexcept

Move Assignment operator from another variant of the same alternative types.

If the other type is valueless, this variant will also be valueless.

Precondition
Only participates in overload resolution if every type in Ts is move-assignable.
Parameters
rhsthe other variant to move from

◆ valueless_by_exception()

template<typename... Ts>
auto arene::base::variant< Ts >::valueless_by_exception ( ) const -> bool
inlineconstexprnoexcept

Check if the variant is valueless.

A variant becomes valueless if and only if an exception is thrown during copy or move initialization

Returns
true if the variant does not hold a value
false if the variant holds a value.

Friends And Related Symbol Documentation

◆ operator<

template<typename... Ts>
auto operator< ( variant< Ts > const & lhs,
variant< Ts > const & rhs ) -> bool
friend

Less-than comparison.

Precondition
The behavior is undefined if not all the alternatives support less than comparison.
Parameters
lhsvariant to compare
rhsthe other variant to compare
Returns
false if either variant is valueless
Otherwise, returns the result of invoking std::less on the held alternatives of the two variants.

◆ operator==

template<typename... Ts>
auto operator== ( variant< Ts > const & lhs,
variant< Ts > const & rhs ) -> bool
friend

Equality comparison.

Precondition
The behavior is undefined if not all the alternatives support equality comparison.
Parameters
lhsvariant to compare
rhsthe other variant to compare
Returns
true if both variants are valueless, or both variants hold the same alternative type and the value of that alternative type is equal.
false otherwise.

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