--- title: "variant (legacy)" --- {{< callout type="info">}} Header: `variant.h` Similar to: `std::variant` {{< /callout >}} {{< callout type="warning">}} Deprecated for C++11 and above. This version is automatically selected if either the compiler does not support C++11, or `ETL_USE_LEGACY_VARIANT` is defined. {{< /callout >}} 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. Accepts etl::visit since `20.28.0`. If `ETL_USE_LEGACY_VARIANT` is defined then `etl::variant` is in the `etl` namespace. If `ETL_USE_LEGACY_VARIANT` is not defined then `etl::variant` is in the `etl::legacy` namespace. Since: `20.30.0`. ## Constructors ```cpp variant() ``` **Description** Default constructor. --- ```cpp template variant(T value) ``` **Description** Construct from value. A static assert will occur if `T` is not a supported type. ## Operations ```cpp template variant& operator =(typename parameter_type::type value) ``` **Description** Assigns a value. A static assert occurs if the type `T` is not supported. ### If C++11 is **not** supported ```cpp template T& emplace(const TP1& value1) template T& emplace(const TP1& value1, const TP2& value2) template T& emplace(const TP1& value1, const TP2& value2, const TP3& value3) template T& emplace(const TP1& value1, const TP2& value2, const TP3& value3, const TP4& value4) ``` **Description** Construct a `T` from multiple arguments. ### If C++11 or greater is supported ```cpp template T& emplace(Args&&... args) ``` **Description** Construct a `T`s in-place using the supplied parameters. --- ```cpp void clear() ``` **Description** Clears the variant of holding any type. ## Access ```cpp template T& get() ``` 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 raised. If asserts or exceptions are not enabled then undefined behaviour occurs. --- ```cpp const template 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 raised. If asserts or exceptions are not enabled then undefined behaviour occurs. --- ```cpp T* upcast_ptr() const T* upcast_ptr() const ``` For variant types that are polymorphic. Up-casts the variant to type `T*`. Types that cannot be up-cast will result in a `nullptr` return. Since: `20.30.0` --- ```cpp T& upcast() const T& upcast() const ``` For variant types that are polymorphic. Up-casts the variant to type` T`. Types that cannot be up-cast will result in a runtime error `etl::variant_not_a_base`. Since: `20.30.0` --- ```cpp bool is_base_of() const ``` Tests if the type `T` is a base of the current variant type. Since: `20.30.0` --- ```cpp call(reader& r) ``` Calls the appropriate overloaded `r.read()` function with the value. --- ```cpp template variant& operator =(typename parameter_type::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 `etl::variant_incorrect_type_exception` error is raised. ## Tests ```cpp template static bool is_supported_type() ``` Returns true if `T` is supported by the variant, otherwise `false`. --- ```cpp bool is_same_type(const variant& other) const ``` Returns `true` if the variant holds the same type as the other, otherwise `false`. --- ```cpp bool is_valid() const ``` Returns `true` if the variant holds a valid value, otherwise `false`. --- ```cpp template bool is_type() const ``` Returns `true` if `T` is the type currently stored in the variant, otherwise `false`. ## Visitation For STL style `etl::visit` `20.28.0` The ETL implements an STL style visit function.