A C++ template library for embedded applications
MIT licensed
Designed and
maintained by
John Wellbelove
Support the development
of the ETL

variant (variadic)

For C++11 and above.

STL equivalent:- std::variant


template <typename... TTypes>
class variant

A variadic class that can contain one a several specified types in a type safe manner.
C++11 and C++14 support up to 32 types for visitor callback callbacks.
C++17 and above supports any number of visitor callback types.

Accepts functors, etl::overload and etl::visitor types as visitor callbacks.
____________________________________________________________________________________________________

Error exception types


class variant_exception : public exception

class variant_incorrect_type_exception : public variant_exception
The error types emitted when a non-supported type is requested from the variant.
____________________________________________________________________________________________________

Construction

____________________________________________________________________________________________________
ETL_CONSTEXPR14 variant()
Default constructor.
____________________________________________________________________________________________________
template <typename T>
ETL_CONSTEXPR14 variant(T&& value)
Constructor from a value.
____________________________________________________________________________________________________
template <typename T, typename... TArgs>
ETL_CONSTEXPR14 explicit variant(etl::in_place_type_t<T>, TArgs&&... args)
Construct T from arguments.
____________________________________________________________________________________________________
template <size_t Index, typename... TArgs>
ETL_CONSTEXPR14 explicit variant(etl::in_place_index_t<Index>, TArgs&&... args)
Construct from Index using  args.
____________________________________________________________________________________________________
template <typename T, typename U, typename... TArgs>
ETL_CONSTEXPR14 explicit variant(etl::in_place_type_t<T>,
                                 std::initializer_list<U> init,
                                 TArgs&&... args)
Construct from type, initializer_list and arguments.
____________________________________________________________________________________________________
template <size_t Index, typename U, typename... TArgs>
ETL_CONSTEXPR14 explicit variant(etl::in_place_index_t<Index>,
                                 std::initializer_list<U> init,
                                 TArgs&&... args)
Construct from Index, initializer_list and arguments.
____________________________________________________________________________________________________
ETL_CONSTEXPR14 variant(const variant& other)
Copy constructor.
____________________________________________________________________________________________________
ETL_CONSTEXPR14 variant(variant&& other)
Move constructor.
____________________________________________________________________________________________________

Operations

____________________________________________________________________________________________________
template <typename T, typename... TArgs>
T& emplace(TArgs&&... args)
Set to type T using the construction parameters args.
____________________________________________________________________________________________________
template <typename T>
variant& operator =(T&& value)
____________________________________________________________________________________________________   
variant& operator =(const variant& other)
Copies other to the current variant.
____________________________________________________________________________________________________
variant& operator =(variant&& other)
Moves other to the current variant.
____________________________________________________________________________________________________
constexpr bool valueless_by_exception() const noexcept
Returns true if the type id is etl::variant_npos.
____________________________________________________________________________________________________
constexpr size_t index() const noexcept
Returns the index of the type contained in the variant.
Returns etl::variant_npos if the variant does not hold a valid type.
____________________________________________________________________________________________________
void swap(variant& rhs) noexcept
Swaps rhs with the current variant.
____________________________________________________________________________________________________

Access

Non-member functions

____________________________________________________________________________________________________

get


____________________________________________________________________________________________________
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 etl::variant_alternative_t<Index, etl::variant<TTypes...>>&
  get(etl::variant<TTypes...>& v)
____________________________________________________________________________________________________
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 etl::variant_alternative_t<Index, etl::variant<TTypes...>>&&
  get(etl::variant<TTypes...>&& v)
____________________________________________________________________________________________________
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 const etl::variant_alternative_t<Index, const etl::variant<TTypes...>>&
  get(const etl::variant<TTypes...>& v)
____________________________________________________________________________________________________
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 const etl::variant_alternative_t<Index, const etl::variant<TTypes...>>&&
  get(const etl::variant<TTypes...>&& v)
____________________________________________________________________________________________________
template <typenameT, typename... TTypes>
ETL_CONSTEXPR14 T& get(etl::variant<TTypes...>& v)
____________________________________________________________________________________________________
template <typenameT, typename... TTypes>
ETL_CONSTEXPR14 T&& get(etl::variant<TTypes...>&& v)
____________________________________________________________________________________________________
template <typenameT, typename... TTypes>
ETL_CONSTEXPR14 const T& get(const etl::variant<TTypes...>& v)
____________________________________________________________________________________________________
template <typenameT, typename... TTypes>
ETL_CONSTEXPR14 const T&& get(const etl::variant<TTypes...>&& v)
____________________________________________________________________________________________________

get_if

____________________________________________________________________________________________________
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 etl::add_pointer_t<etl::variant_alternative_t<Index, etl::variant<TTypes...>>>
  get_if(etl::variant<TTypes...>* pv) noexcept
____________________________________________________________________________________________________
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 etl::add_pointer_t<const etl::variant_alternative_t<Index, etl::variant<TTypes...>>>
  get_if(const etl::variant<TTypes...>* pv) noexcept
____________________________________________________________________________________________________
template <typename T, typename... TTypes>
ETL_CONSTEXPR14 etl::add_pointer_t<T> get_if(etl::variant<TTypes...>* pv) noexcept
____________________________________________________________________________________________________
template <typename T, typename... TTypes>
ETL_CONSTEXPR14 etl::add_pointer_t<const T> get_if(const etl::variant<TTypes...>* pv) noexcept
____________________________________________________________________________________________________

Other


struct monostate
A default constructible type.
For use as the first state to turn a non-default constructible variant into a default constructible variant.
____________________________________________________________________________________________________
template <typename... TTypes>
void swap(etl::variant<TTypes...>& lhs, etl::variant<TTypes...>& rhs)
Swaps lhs and rhs.
____________________________________________________________________________________________________
template <typename... TTypes>
struct variant_size<etl::variant<TTypes...>>
The member constant value contains the number of types.

For C++17 and above.

template <typename... TTypes>
inline constexpr size_t variant_size_v = variant_size<TTypes...>::value;
Has the value equal to the  number of types.
____________________________________________________________________________________________________

Tests

Non-member functions

____________________________________________________________________________________________________
template <typename T, typename... TTypes>
ETL_CONSTEXPR14 bool holds_alternative(const etl::variant<TTypes...>& v) noexcept
Checks if the variant v holds the alternative T.
____________________________________________________________________________________________________
template <size_t Index, typename... TTypes>
ETL_CONSTEXPR14 bool holds_alternative(const etl::variant<TTypes...>& v) noexcept
Checks if the variant v holds the alternative Index.
____________________________________________________________________________________________________
template <typename... TTypes>
ETL_CONSTEXPR14 bool holds_alternative(size_t index, const etl::variant<TTypes...>& v) noexcept
Checks if the variant v holds the alternative index.
____________________________________________________________________________________________________

Visitation


For C++11 and C++14 the maximum number of visitor types is 32.
Code size may be minimised if one of the following macros are defined.
ETL_VARIANT_CPP11_MAX_8_TYPES
ETL_VARIANT_CPP11_MAX_16_TYPES
ETL_VARIANT_CPP11_MAX_24_TYPES

C++17 and above support any number of visitor callback types.

etl::variant provides member functions that support two methods of visitation.
____________________________________________________________________________________________________
void accept_visitor(etl::visitor<TTypes...>& v)
Accepts an etl::visitor derived type.

See visitor

____________________________________________________________________________________________________
template <typename TVisitor>
void accept_functor(TVisitor& v)
Accepts a functor that overrides operator() for each type.

See overload
variant.h