Variants
(ply-system.h)
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;
template <typename T> Variant::Variant(T&& value)Constructs a variant containing the given value.
Tmust be one of the variant's allowed types.template <typename T> Variant& Variant::operator=(T&& value)Assigns a new value to the variant. The previous value is destroyed first.
u32 Variant::getSubtypeIndex() constReturns the zero-based index of the currently held type within the variant's type list.
bool Variant::isEmpty() constReturns
trueif the variant holds no value.template <typename T> bool Variant::is() constReturns
trueif the variant currently holds a value of typeT.template <typename T> T* Variant::as()
template <typename T> const T* Variant::as() constReturns a pointer to the contained value if it's of type
T, ornullptrotherwise.template <typename T, typename... Args> T& Variant::switchTo(Args&& args)Destroys the current value (if any), constructs a new value of type
Tusing the provided arguments, and returns a reference to it.
Variant<int, String, float> value;
value = 42;
if (value.is<int>()) {
int* p = value.as<int>(); // Points to the int.
}
value.switchTo<String>("hello");