Template Class Variant#

Class Documentation#

template<typename ...V>
class Variant#

A typed variant container.

Note

Variant is always backed by a single Any (TVMFFIAny). Even when every alternative derives from ObjectRef, Variant is not ObjectRef-derived; this keeps the layout independent of the contained types (sizeof(Variant<…>) == sizeof(Any)).

Public Functions

Variant(const Variant<V...> &other) = default#

Constructor from another variant.

Parameters:

other – The other variant

Variant(Variant<V...> &&other) noexcept = default#

Constructor from another variant.

Parameters:

other – The other variant

template<typename T, typename = enable_if_variant_contains_t<T>>
inline Variant(T other)#

Constructor from a contained value.

Parameters:

other – The value to store

template<typename T, typename = enable_if_variant_contains_t<T>>
inline std::optional<T> as() const#

Try to cast to a type T, return std::nullopt if the cast is not possible.

Template Parameters:

T – The type to cast to.

Returns:

The casted value, or std::nullopt if the cast is not possible.

template<typename T, typename = std::enable_if_t<std::is_base_of_v<Object, T>>>
inline const T *as() const#

Shortcut of as Object to cast to a const pointer when T is an Object.

Template Parameters:

T – The object type.

Returns:

The requested pointer, returns nullptr if type mismatches.

template<typename T, typename = enable_if_variant_contains_t<T>>
inline T get() const &#

Get the value of the variant in type T, throws an exception if cast fails.

Template Parameters:

T – The type to get.

Returns:

The value of the variant

template<typename T, typename = enable_if_variant_contains_t<T>>
inline T get() &&#

Get the value of the variant in type T, throws an exception if cast fails.

Template Parameters:

T – The type to get.

Returns:

The value of the variant

inline std::string GetTypeKey() const#

Get the type key of the variant.

Returns:

The type key of the variant

inline bool same_as(const Variant<V...> &other) const#

Shallow-compare with another variant.

Parameters:

other – The other variant.

Returns:

Whether the two hold the same underlying value.