CETL 0.0.0
 
Loading...
Searching...
No Matches
cetl::variant< Ts > Class Template Reference

#include "cetl/pf17/variant.hpp"

Public Member Functions

template<typename T = nth_type<0>, std::enable_if_t< std::is_default_constructible< T >::value, int > = 0>
constexpr variant () noexcept(std::is_nothrow_default_constructible< nth_type< 0 > >::value)
 
constexpr variant (const variant &other)=default
 
constexpr variant (variant &&other) noexcept(tys::nothrow_move_constructible)=default
 
template<typename U, std::size_t Ix = detail::var::best_converting_ctor_index_v<U, Ts...>, std::enable_if_t<(Ix< std::numeric_limits< std::size_t >::max()), int > = 0, typename Alt = nth_type<Ix>, std::enable_if_t< std::is_constructible< Alt, U >::value, int > = 0, std::enable_if_t<!std::is_same< std::decay_t< U >, variant >::value, int > = 0, std::enable_if_t<!detail::is_in_place_type< std::decay_t< U > >::value, int > = 0, std::enable_if_t<!detail::is_in_place_index< std::decay_t< U > >::value, int > = 0>
constexpr variant (U &&from) noexcept(std::is_nothrow_constructible< nth_type< Ix >, U >::value)
 
template<typename T, std::size_t Ix = index_of<T>, typename... Args, std::enable_if_t< is_unique< T > &&std::is_constructible< T, Args... >::value, int > = 0>
constexpr variant (const in_place_type_t< T >, Args &&... args)
 
template<typename T, std::size_t Ix = index_of<T>, typename U, typename... Args, std::enable_if_t< is_unique< T > &&std::is_constructible< T, std::initializer_list< U > &, Args... >::value, int > = 0>
constexpr variant (const in_place_type_t< T >, const std::initializer_list< U > il, Args &&... args)
 
template<std::size_t Ix, typename... Args, std::enable_if_t<(Ix< sizeof...(Ts)) &&std::is_constructible< nth_type< Ix >, Args... >::value, int > = 0>
constexpr variant (const in_place_index_t< Ix >, Args &&... args)
 
template<std::size_t Ix, typename U, typename... Args, std::enable_if_t<(Ix< sizeof...(Ts)) &&std::is_constructible< nth_type< Ix >, std::initializer_list< U > &, Args... >::value, int > = 0>
constexpr variant (const in_place_index_t< Ix >, const std::initializer_list< U > il, Args &&... args)
 
constexpr variantoperator= (const variant &rhs)=default
 
constexpr variantoperator= (variant &&rhs) noexcept(tys::nothrow_move_constructible &&tys::nothrow_move_assignable)=default
 
template<typename U, std::size_t Ix = detail::var::best_converting_assignment_index_v<U, Ts...>, std::enable_if_t<(Ix< std::numeric_limits< std::size_t >::max()), int > = 0, typename Alt = nth_type<Ix>, std::enable_if_t< std::is_constructible< Alt, U >::value &&std::is_assignable< Alt &, U >::value, int > = 0, std::enable_if_t<!std::is_same< std::decay_t< U >, variant >::value, int > = 0, std::enable_if_t<!detail::is_in_place_type< std::decay_t< U > >::value, int > = 0, std::enable_if_t<!detail::is_in_place_index< std::decay_t< U > >::value, int > = 0>
variantoperator= (U &&from) noexcept(std::is_nothrow_constructible< Alt, U >::value &&std::is_nothrow_assignable< Alt &, U >::value)
 
constexpr void swap (variant &other) noexcept(tys::nothrow_move_constructible &&tys::nothrow_swappable)
 
CETL_NODISCARD constexpr std::size_t index () const noexcept
 
CETL_NODISCARD constexpr bool valueless_by_exception () const noexcept
 
template<typename T, typename... Args, std::size_t Ix = index_of<T>, std::enable_if_t< std::is_constructible< T, Args... >::value, int > = 0>
T & emplace (Args &&... ar)
 
template<typename T, typename U, typename... Args, std::size_t Ix = index_of<T>, std::enable_if_t< std::is_constructible< T, Args... >::value, int > = 0>
T & emplace (const std::initializer_list< U > il, Args &&... ar)
 
template<std::size_t Ix, typename... Args, std::enable_if_t<(Ix< sizeof...(Ts)), int > = 0, typename T = variant_alternative_t<Ix, variant>, std::enable_if_t< std::is_constructible< T, Args... >::value, int > = 0>
T & emplace (Args &&... ar)
 
template<std::size_t Ix, typename U, typename... Args, std::enable_if_t<(Ix< sizeof...(Ts)), int > = 0, typename T = variant_alternative_t<Ix, variant>, std::enable_if_t< std::is_constructible< T, Args... >::value, int > = 0>
T & emplace (const std::initializer_list< U > il, Args &&... ar)
 

Friends

template<std::size_t Ix, typename... Us>
constexpr std::add_pointer_t< variant_alternative_t< Ix, variant< Us... > > > get_if (variant< Us... > *const var) noexcept
 
template<std::size_t Ix, typename... Us>
constexpr std::add_pointer_t< const variant_alternative_t< Ix, variant< Us... > > > get_if (const variant< Us... > *const var) noexcept
 
template<std::size_t Ix, typename... Us>
constexpr variant_alternative_t< Ix, variant< Us... > > & get (variant< Us... > &var)
 
template<std::size_t Ix, typename... Us>
constexpr variant_alternative_t< Ix, variant< Us... > > && get (variant< Us... > &&var)
 
template<std::size_t Ix, typename... Us>
constexpr const variant_alternative_t< Ix, variant< Us... > > & get (const variant< Us... > &var)
 
template<std::size_t Ix, typename... Us>
constexpr const variant_alternative_t< Ix, variant< Us... > > && get (const variant< Us... > &&var)
 
template<typename Fun, typename V>
constexpr decltype(auto) detail::var::visit (Fun &&fun, V &&var)
 

Detailed Description

template<typename... Ts>
class cetl::variant< Ts >

An implementation of std::variant.

The valueless state cannot occur unless exceptions are enabled.

In this implementation, the address of the variant object is equivalent to the address of the active alternative; this can sometimes be useful for low-level debugging.

Template Parameters
TsThe list of alternatives. At least one alternative is required.

Constructor & Destructor Documentation

◆ variant() [1/8]

template<typename... Ts>
template<typename T = nth_type<0>, std::enable_if_t< std::is_default_constructible< T >::value, int > = 0>
cetl::pf17::variant< Ts >::variant ( )
inlineconstexprnoexcept

Constructor 1 – default constructor.

◆ variant() [2/8]

template<typename... Ts>
cetl::pf17::variant< Ts >::variant ( const variant< Ts > & other)
constexprdefault

Constructor 2 – copy constructor.

◆ variant() [3/8]

template<typename... Ts>
cetl::pf17::variant< Ts >::variant ( variant< Ts > && other) const
constexprdefaultnoexcept

Constructor 3 – move constructor.

◆ variant() [4/8]

template<typename... Ts>
template<typename U, std::size_t Ix = detail::var::best_converting_ctor_index_v<U, Ts...>, std::enable_if_t<(Ix< std::numeric_limits< std::size_t >::max()), int > = 0, typename Alt = nth_type<Ix>, std::enable_if_t< std::is_constructible< Alt, U >::value, int > = 0, std::enable_if_t<!std::is_same< std::decay_t< U >, variant >::value, int > = 0, std::enable_if_t<!detail::is_in_place_type< std::decay_t< U > >::value, int > = 0, std::enable_if_t<!detail::is_in_place_index< std::decay_t< U > >::value, int > = 0>
cetl::pf17::variant< Ts >::variant ( U && from)
inlineconstexprnoexcept

Constructor 4 – converting constructor.

◆ variant() [5/8]

template<typename... Ts>
template<typename T, std::size_t Ix = index_of<T>, typename... Args, std::enable_if_t< is_unique< T > &&std::is_constructible< T, Args... >::value, int > = 0>
cetl::pf17::variant< Ts >::variant ( const in_place_type_t< T > ,
Args &&... args )
inlineexplicitconstexpr

Constructor 5.

◆ variant() [6/8]

template<typename... Ts>
template<typename T, std::size_t Ix = index_of<T>, typename U, typename... Args, std::enable_if_t< is_unique< T > &&std::is_constructible< T, std::initializer_list< U > &, Args... >::value, int > = 0>
cetl::pf17::variant< Ts >::variant ( const in_place_type_t< T > ,
const std::initializer_list< U > il,
Args &&... args )
inlineexplicitconstexpr

Constructor 6.

◆ variant() [7/8]

template<typename... Ts>
template<std::size_t Ix, typename... Args, std::enable_if_t<(Ix< sizeof...(Ts)) &&std::is_constructible< nth_type< Ix >, Args... >::value, int > = 0>
cetl::pf17::variant< Ts >::variant ( const in_place_index_t< Ix > ,
Args &&... args )
inlineexplicitconstexpr

Constructor 7.

◆ variant() [8/8]

template<typename... Ts>
template<std::size_t Ix, typename U, typename... Args, std::enable_if_t<(Ix< sizeof...(Ts)) &&std::is_constructible< nth_type< Ix >, std::initializer_list< U > &, Args... >::value, int > = 0>
cetl::pf17::variant< Ts >::variant ( const in_place_index_t< Ix > ,
const std::initializer_list< U > il,
Args &&... args )
inlineexplicitconstexpr

Constructor 8.

Member Function Documentation

◆ emplace() [1/4]

template<typename... Ts>
template<typename T, typename... Args, std::size_t Ix = index_of<T>, std::enable_if_t< std::is_constructible< T, Args... >::value, int > = 0>
T & cetl::pf17::variant< Ts >::emplace ( Args &&... ar)
inline

These methods only participate in overload resolution if the template parameters are valid.

The type-based overloads only participate if the type is unique in the variant. If the constructor throws, the variant becomes valueless.

◆ emplace() [2/4]

template<typename... Ts>
template<std::size_t Ix, typename... Args, std::enable_if_t<(Ix< sizeof...(Ts)), int > = 0, typename T = variant_alternative_t<Ix, variant>, std::enable_if_t< std::is_constructible< T, Args... >::value, int > = 0>
T & cetl::pf17::variant< Ts >::emplace ( Args &&... ar)
inline

These methods only participate in overload resolution if the template parameters are valid.

The type-based overloads only participate if the type is unique in the variant. If the constructor throws, the variant becomes valueless.

◆ emplace() [3/4]

template<typename... Ts>
template<typename T, typename U, typename... Args, std::size_t Ix = index_of<T>, std::enable_if_t< std::is_constructible< T, Args... >::value, int > = 0>
T & cetl::pf17::variant< Ts >::emplace ( const std::initializer_list< U > il,
Args &&... ar )
inline

These methods only participate in overload resolution if the template parameters are valid.

The type-based overloads only participate if the type is unique in the variant. If the constructor throws, the variant becomes valueless.

◆ emplace() [4/4]

template<typename... Ts>
template<std::size_t Ix, typename U, typename... Args, std::enable_if_t<(Ix< sizeof...(Ts)), int > = 0, typename T = variant_alternative_t<Ix, variant>, std::enable_if_t< std::is_constructible< T, Args... >::value, int > = 0>
T & cetl::pf17::variant< Ts >::emplace ( const std::initializer_list< U > il,
Args &&... ar )
inline

These methods only participate in overload resolution if the template parameters are valid.

The type-based overloads only participate if the type is unique in the variant. If the constructor throws, the variant becomes valueless.

◆ index()

template<typename... Ts>
CETL_NODISCARD constexpr std::size_t cetl::pf17::variant< Ts >::index ( ) const
inlineconstexprnoexcept

The index of the currently held alternative, or variant_npos if the variant is valueless.

◆ operator=() [1/3]

template<typename... Ts>
variant & cetl::pf17::variant< Ts >::operator= ( const variant< Ts > & rhs)
constexprdefault

Assignment 1 – copy assignment If the current alternative is different and the new alternative is not nothrow-move-constructible and the copy constructor throws, the variant becomes valueless.

If nothrow move construction is possible, an intermediate temporary copy will be constructed to avoid the valueless outcome even if the copy ctor throws. This is constexpr only if all Ts are trivially copy assignable.

◆ operator=() [2/3]

template<typename... Ts>
template<typename U, std::size_t Ix = detail::var::best_converting_assignment_index_v<U, Ts...>, std::enable_if_t<(Ix< std::numeric_limits< std::size_t >::max()), int > = 0, typename Alt = nth_type<Ix>, std::enable_if_t< std::is_constructible< Alt, U >::value &&std::is_assignable< Alt &, U >::value, int > = 0, std::enable_if_t<!std::is_same< std::decay_t< U >, variant >::value, int > = 0, std::enable_if_t<!detail::is_in_place_type< std::decay_t< U > >::value, int > = 0, std::enable_if_t<!detail::is_in_place_index< std::decay_t< U > >::value, int > = 0>
variant & cetl::pf17::variant< Ts >::operator= ( U && from)
inlinenoexcept

Assignment 3 – converting assignment.

◆ operator=() [3/3]

template<typename... Ts>
variant & cetl::pf17::variant< Ts >::operator= ( variant< Ts > && rhs) const &&
constexprdefaultnoexcept

Assignment 2 – move assignment If the current alternative is different and the move constructor throws, the variant becomes valueless.

This is constexpr only if all Ts are trivially move assignable.

◆ swap()

template<typename... Ts>
void cetl::pf17::variant< Ts >::swap ( variant< Ts > & other)
inlineconstexprnoexcept

Invokes swap() on the Ts if the indices match, otherwise swaps the variants.

In the first case, if an exception is thrown, the state of the values depends on the exception safety of the swap function called. In the second case, if the move constructor or the move assignment operator throw, either or both variants may become valueless.

◆ valueless_by_exception()

template<typename... Ts>
CETL_NODISCARD constexpr bool cetl::pf17::variant< Ts >::valueless_by_exception ( ) const
inlineconstexprnoexcept

True if the variant is valueless.


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