UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
TVariant< T, Ts > Class Template Referencefinal

#include <TVariant.h>

+ Inheritance diagram for TVariant< T, Ts >:

Public Member Functions

 TVariant ()
 
template<typename U , typename... ArgTypes>
 TVariant (TInPlaceType< U > &&, ArgTypes &&... Args)
 
 TVariant (const TVariant &Other)
 
 TVariant (TVariant &&Other)
 
TVariantoperator= (const TVariant &Other)
 
TVariantoperator= (TVariant &&Other)
 
 ~TVariant ()=default
 
template<typename U >
bool IsType () const
 
template<typename U >
U & Get () UE_LIFETIMEBOUND
 
template<typename U >
const U & Get () const UE_LIFETIMEBOUND
 
template<typename U >
const U & Get (const TIdentity_T< U > &DefaultValue UE_LIFETIMEBOUND) const UE_LIFETIMEBOUND
 
template<typename U >
U * TryGet () UE_LIFETIMEBOUND
 
template<typename U >
const U * TryGet () const UE_LIFETIMEBOUND
 
template<typename U >
void Set (typename TIdentity< U >::Type &&Value)
 
template<typename U >
void Set (const typename TIdentity< U >::Type &Value)
 
template<typename U , typename... ArgTypes>
void Emplace (ArgTypes &&... Args)
 
SIZE_T GetIndex () const
 
template<typename U >
 TVariant (UE::Core::Private::TUninitializedType< U > &&)
 

Static Public Member Functions

template<typename U >
static constexpr SIZE_T IndexOfType ()
 

Detailed Description

template<typename T, typename... Ts>
class TVariant< T, Ts >

A type-safe union based loosely on std::variant. This flavor of variant requires that all the types in the declaring template parameter pack be unique. Attempting to use the value of a Get() when the underlying type is different leads to undefined behavior.

Constructor & Destructor Documentation

◆ TVariant() [1/5]

template<typename T , typename... Ts>
TVariant< T, Ts >::TVariant ( )
inline

Default initialize the TVariant to the first type in the parameter pack

◆ TVariant() [2/5]

template<typename T , typename... Ts>
template<typename U , typename... ArgTypes>
TVariant< T, Ts >::TVariant ( TInPlaceType< U > &&  ,
ArgTypes &&...  Args 
)
inlineexplicit

Perform in-place construction of a type into the variant

◆ TVariant() [3/5]

template<typename T , typename... Ts>
TVariant< T, Ts >::TVariant ( const TVariant< T, Ts > &  Other)
inline

Copy construct the variant from another variant of the same type

◆ TVariant() [4/5]

template<typename T , typename... Ts>
TVariant< T, Ts >::TVariant ( TVariant< T, Ts > &&  Other)
inline

Move construct the variant from another variant of the same type

◆ ~TVariant()

template<typename T , typename... Ts>
TVariant< T, Ts >::~TVariant ( )
default

◆ TVariant() [5/5]

template<typename T , typename... Ts>
template<typename U >
TVariant< T, Ts >::TVariant ( UE::Core::Private::TUninitializedType< U > &&  )
inlineexplicit

INTERNAL USE ONLY – Do not call this constructor, it will put the variant in a bad state. Its internal use case is for in-place construction of non-default-constructible types during serialization.

Construct the TVariant to store the specified uninitialized element type. The caller must unconditionally get a pointer to the element and construct an object of the right type in that position. This is all totally exception-unsafe. This relies on undefined behavior by dereferencing a pointer to an object (inside Get) that doesn't exist, and then turning it back to an pointer. Any other attempts to use or destroy the variant before an object of the right type has been constructed is completely unsafe, it may e.g. result in trying to destroy an object that is not there.

Member Function Documentation

◆ Emplace()

template<typename T , typename... Ts>
template<typename U , typename... ArgTypes>
void TVariant< T, Ts >::Emplace ( ArgTypes &&...  Args)
inline

Set a specifically-typed value into the variant using in-place construction

◆ Get() [1/3]

template<typename T , typename... Ts>
template<typename U >
const U & TVariant< T, Ts >::Get ( ) const
inline

Get a reference to the held value. Bad things can happen if this is called on a variant that does not hold the type asked for

◆ Get() [2/3]

template<typename T , typename... Ts>
template<typename U >
U & TVariant< T, Ts >::Get ( )
inline

Get a reference to the held value. Bad things can happen if this is called on a variant that does not hold the type asked for

◆ Get() [3/3]

template<typename T , typename... Ts>
template<typename U >
const U & TVariant< T, Ts >::Get ( const TIdentity_T< U > &DefaultValue  UE_LIFETIMEBOUND) const
inline

Get a reference to the held value if set, otherwise the DefaultValue

◆ GetIndex()

template<typename T , typename... Ts>
SIZE_T TVariant< T, Ts >::GetIndex ( ) const
inline

Returns the currently held type's index into the template parameter pack

◆ IndexOfType()

template<typename T , typename... Ts>
template<typename U >
static constexpr SIZE_T TVariant< T, Ts >::IndexOfType ( )
inlinestaticconstexpr

Lookup the index of a type in the template parameter pack at compile time.

◆ IsType()

template<typename T , typename... Ts>
template<typename U >
bool TVariant< T, Ts >::IsType ( ) const
inline

Determine if the variant holds the specific type

◆ operator=() [1/2]

template<typename T , typename... Ts>
TVariant & TVariant< T, Ts >::operator= ( const TVariant< T, Ts > &  Other)
inline

Copy assign a variant from another variant of the same type

◆ operator=() [2/2]

template<typename T , typename... Ts>
TVariant & TVariant< T, Ts >::operator= ( TVariant< T, Ts > &&  Other)
inline

Move assign a variant from another variant of the same type

◆ Set() [1/2]

template<typename T , typename... Ts>
template<typename U >
void TVariant< T, Ts >::Set ( const typename TIdentity< U >::Type &  Value)
inline

Set a specifically-typed value into the variant

◆ Set() [2/2]

template<typename T , typename... Ts>
template<typename U >
void TVariant< T, Ts >::Set ( typename TIdentity< U >::Type &&  Value)
inline

Set a specifically-typed value into the variant

◆ TryGet() [1/2]

template<typename T , typename... Ts>
template<typename U >
const U * TVariant< T, Ts >::TryGet ( ) const
inline

Get a pointer to the held value if the held type is the same as the one specified

◆ TryGet() [2/2]

template<typename T , typename... Ts>
template<typename U >
U * TVariant< T, Ts >::TryGet ( )
inline

Get a pointer to the held value if the held type is the same as the one specified


The documentation for this class was generated from the following file: