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' 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.

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.

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