<ply-base.h>
namespace ply
Variant Types

A Variant can hold a value of one of several predefined types at runtime. It's similar to a tagged union.

template <typename... Types> class Variant;
Additional Constructors
template <typename T
Variant(T&& value)
Additional Assignment Operators
template <typename T
Variantoperator=(T&& value)
Item access
u32 get_subtype_index() const
bool is_empty() const
template <typename T
bool is() const
template <typename T
Tas()
template <typename T
const Tas() const
Modification
template <typename T,  typename... Args
Tswitch_to(Args&& args)
template <typename TVariant(T&& value)

Constructs a variant containing the given value. T must be one of the variant's allowed types.

template <typename TVariantoperator=(T&& value)

Assigns a new value to the variant. The previous value is destroyed first.

u32 get_subtype_index() const

Returns the zero-based index of the currently held type within the variant's type list.

bool is_empty() const

Returns true if the variant holds no value.

template <typename Tbool is() const

Returns true if the variant currently holds a value of type T.

template <typename TTas()
template <typename T> const Tas() const

Returns a pointer to the contained value if it's of type T, or nullptr otherwise.

template <typename T,  typename... ArgsTswitch_to(Args&& args)

Destroys the current value (if any), constructs a new value of type T using the provided arguments, and returns a reference to it.

Example
Variant<int, String, float> value;
value = 42;
if (value.is<int>()) {
    int* p = value.as<int>();  // p points to the int
}
value.switch_to<String>("hello");