A C++ template library for embedded applications
Designed and maintained by
Aster Consulting Ltd

variant

A class that can contain one a several specified types in a type safe manner.
Supports up to eight types.
Supplies a nested 'reader' visitor class that allows type safe access via sets of overloaded virtual 'read' functions for each
type.

Constructor

variant()
Default constructor.

template <typename T>
variant(T value)
Construct from value. A static assert will occur if T is not a supported type.

Operations

template <typename T>
variant& operator =(typename parameter_type<T>::type value)
Assigns a value. A static assert occurs if the type T is not supported.

If C++11 is not supported
template <typename T, typename TP1>
T& emplace(const TP1& value1)

template <typename T, typename TP1, typename TP2>
T& emplace(const TP1& value1, const TP2& value2)

template <typename T, typename TP1, typename TP2, typename TP3>
T& emplace(const TP1& value1, const TP2& value2, const TP3& value3)

template <typename T, typename TP1, typename TP2, typename TP3, typename TP4>
T& emplace(const TP1& value1, const TP2& value2, const TP3& value3, const TP4& value4)

If C++11 or greater is supported
template <typename T, typename... Args>
T& emplace(Args&&... args)
Constructs the value in-place using the supplied parameters.

void clear()
Clears the variant of holding any type.

Access

template <typename T>
T& get()
const template <typename T>
T& get() const
Gets a value.
If T is a type not supported by the variant then a static assert occurs.
If T is  a type that is not the type currently stored then a variant_incorrect_type_exception() error is emitted. If
asserts or exceptions are not enabled then undefined behaviour occurs.

upcast<T>()
For variant types that are polymorphic. Up-casts the variant to type T.
Types that cannot be up-cast will result in a compiler error.

Call(reader& r)
Calls the appropriate overloaded r.read()  function with the value.

template <typename T>
variant& operator =(typename parameter_type<T>::type value)
Assigns a value. A static assert occurs if the type T is not supported.

Any value stored in the variant can be implicitly cast to its current type.
If the implicit cast is to a type not supported by the variant then a static assert occurs.
If the implicit cast is to a type that is not the type currently stored then a variant_incorrect_type_exception() error is
emitted.

Tests

template <typename T>
static bool is_supported_type()
Returns true if T is supported by the variant, otherwise false.

bool is_same_type(const variant& other) const
Returns true if the variant holds the same type as the other, otherwise false.

bool is_valid() const
Returns true if the variant holds a valid value, otherwise false.

template <typename T>
bool is_type() const
Returns true if T is the type currently stored in the variant, otherwise false.
variant.h