UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SharedPointer.h File Reference
#include "CoreTypes.h"
#include "Misc/IntrusiveUnsetOptionalState.h"
#include "Templates/PointerIsConvertibleFromTo.h"
#include "Misc/AssertionMacros.h"
#include "HAL/UnrealMemory.h"
#include "Containers/Array.h"
#include "Containers/Map.h"
#include "CoreGlobals.h"
#include "Misc/UEOps.h"
#include "Templates/SharedPointerInternals.h"
#include "Templates/SharedPointerTesting.inl"

Go to the source code of this file.

Classes

class  TSharedRef< ObjectType, InMode >
 
struct  TCallTraits< TSharedRef< ObjectType, Mode > >
 
class  TSharedPtr< ObjectType, InMode >
 
struct  TIsZeroConstructType< TSharedPtr< ObjectType, Mode > >
 
struct  TCallTraits< TSharedPtr< ObjectType, Mode > >
 
class  TWeakPtr< ObjectType, InMode >
 
struct  TIsWeakPointerType< TWeakPtr< T, Mode > >
 
struct  TIsZeroConstructType< TWeakPtr< T, Mode > >
 
struct  TCallTraits< TWeakPtr< ObjectType, Mode > >
 
class  TSharedFromThis< ObjectType, Mode >
 

Namespaces

namespace  UE
 
namespace  UE::Core
 
namespace  UE::Core::Private
 implementation
 
namespace  Freeze
 

Functions

template<class CastToType , class CastFromType , ESPMode Mode>
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef (TSharedRef< CastFromType, Mode > const &InSharedRef)
 
template<typename ObjectType , ESPMode Mode>
UE_FORCEINLINE_HINT TSharedRef< ObjectType, Mode > UE::Core::Private::MakeSharedRef (ObjectType *InObject, SharedPointerInternals::TReferenceControllerBase< Mode > *InSharedReferenceCount)
 
 DECLARE_TEMPLATE_INTRINSIC_TYPE_LAYOUT ((template< class ObjectType, ESPMode Mode >),(TSharedPtr< ObjectType, Mode >))
 
template<class CastToType , class CastFromType , ESPMode Mode>
UE_FORCEINLINE_HINT TSharedPtr< CastToType, Mode > StaticCastSharedPtr (TSharedPtr< CastFromType, Mode > const &InSharedPtr)
 
template<class CastToType , class CastFromType , ESPMode Mode>
UE_FORCEINLINE_HINT TWeakPtr< CastToType, Mode > StaticCastWeakPtr (TWeakPtr< CastFromType, Mode > const &InWeakPtr)
 
template<class CastToType , class CastFromType , ESPMode Mode>
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > ConstCastSharedRef (TSharedRef< CastFromType, Mode > const &InSharedRef)
 
template<class CastToType , class CastFromType , ESPMode Mode>
UE_FORCEINLINE_HINT TSharedPtr< CastToType, Mode > ConstCastSharedPtr (TSharedPtr< CastFromType, Mode > const &InSharedPtr)
 
template<class CastToType , class CastFromType , ESPMode Mode>
UE_FORCEINLINE_HINT TWeakPtr< CastToType, Mode > ConstCastWeakPtr (TWeakPtr< CastFromType, Mode > const &InWeakPtr)
 
template<class ObjectType >
SharedPointerInternals::TRawPtrProxy< ObjectType > MakeShareable (ObjectType *InObject)
 
template<class ObjectType , class DeleterType >
SharedPointerInternals::TRawPtrProxyWithDeleter< ObjectType, DeleterType > MakeShareable (ObjectType *InObject, DeleterType &&InDeleter)
 
template<typename InObjectType , ESPMode InMode = ESPMode::ThreadSafe, typename... InArgTypes>
TSharedRef< InObjectType, InModeMakeShared (InArgTypes &&... Args)
 
template<class Type >
void CleanupPointerArray (TArray< TWeakPtr< Type > > &PointerArray)
 
template<class KeyType , class ValueType >
void CleanupPointerMap (TMap< TWeakPtr< KeyType >, ValueType > &PointerMap)
 
template<typename ObjectType , ESPMode Mode>
uint32 GetTypeHash (const TSharedRef< ObjectType, Mode > &InSharedRef)
 
template<typename ObjectType , ESPMode Mode>
uint32 GetTypeHash (const TSharedPtr< ObjectType, Mode > &InSharedPtr)
 
template<typename ObjectType , ESPMode Mode>
uint32 GetTypeHash (const TWeakPtr< ObjectType, Mode > &InWeakPtr)
 

Variables

template<typename T >
constexpr bool TIsTSharedRef_V = false
 
template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedRef_V< TSharedRef< ObjectType, InMode > > = true
 
template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedRef_V< const TSharedRef< ObjectType, InMode > > = true
 
template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedRef_V< volatile TSharedRef< ObjectType, InMode > > = true
 
template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedRef_V< const volatile TSharedRef< ObjectType, InMode > > = true
 
template<typename T >
constexpr bool TIsTSharedPtr_V = false
 
template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedPtr_V< TSharedPtr< ObjectType, InMode > > = true
 
template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedPtr_V< const TSharedPtr< ObjectType, InMode > > = true
 
template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedPtr_V< volatile TSharedPtr< ObjectType, InMode > > = true
 
template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedPtr_V< const volatile TSharedPtr< ObjectType, InMode > > = true
 
template<typename T >
constexpr bool TIsTWeakPtr_V = false
 
template<class ObjectType , ESPMode InMode>
constexpr bool TIsTWeakPtr_V< TWeakPtr< ObjectType, InMode > > = true
 
template<class ObjectType , ESPMode InMode>
constexpr bool TIsTWeakPtr_V< const TWeakPtr< ObjectType, InMode > > = true
 
template<class ObjectType , ESPMode InMode>
constexpr bool TIsTWeakPtr_V< volatile TWeakPtr< ObjectType, InMode > > = true
 
template<class ObjectType , ESPMode InMode>
constexpr bool TIsTWeakPtr_V< const volatile TWeakPtr< ObjectType, InMode > > = true
 

Function Documentation

◆ CleanupPointerArray()

template<class Type >
void CleanupPointerArray ( TArray< TWeakPtr< Type > > &  PointerArray)
inline

Given a TArray of TWeakPtr's, will remove any invalid pointers.

Parameters
PointerArrayThe pointer array to prune invalid pointers out of

◆ CleanupPointerMap()

template<class KeyType , class ValueType >
void CleanupPointerMap ( TMap< TWeakPtr< KeyType >, ValueType > &  PointerMap)
inline

Given a TMap of TWeakPtr's, will remove any invalid pointers. Not the most efficient.

Parameters
PointerMapThe pointer map to prune invalid pointers out of

◆ ConstCastSharedPtr()

template<class CastToType , class CastFromType , ESPMode Mode>
UE_FORCEINLINE_HINT TSharedPtr< CastToType, Mode > ConstCastSharedPtr ( TSharedPtr< CastFromType, Mode > const &  InSharedPtr)

Casts a 'const' shared pointer to 'mutable' shared pointer. (const_cast)

Parameters
InSharedPtrThe shared pointer to cast

◆ ConstCastSharedRef()

template<class CastToType , class CastFromType , ESPMode Mode>
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > ConstCastSharedRef ( TSharedRef< CastFromType, Mode > const &  InSharedRef)

Casts a 'const' shared reference to 'mutable' shared reference. (const_cast)

Parameters
InSharedRefThe shared reference to cast

◆ ConstCastWeakPtr()

template<class CastToType , class CastFromType , ESPMode Mode>
UE_FORCEINLINE_HINT TWeakPtr< CastToType, Mode > ConstCastWeakPtr ( TWeakPtr< CastFromType, Mode > const &  InWeakPtr)

Casts a 'const' weak pointer to 'mutable' weak pointer. (const_cast)

Parameters
InWeakPtrThe weak pointer to cast

◆ DECLARE_TEMPLATE_INTRINSIC_TYPE_LAYOUT()

DECLARE_TEMPLATE_INTRINSIC_TYPE_LAYOUT ( (template< class ObjectType, ESPMode Mode >)  ,
(TSharedPtr< ObjectType, Mode >)   
)

◆ GetTypeHash() [1/3]

template<typename ObjectType , ESPMode Mode>
uint32 GetTypeHash ( const TSharedPtr< ObjectType, Mode > &  InSharedPtr)

Computes a hash code for this object

Parameters
InSharedPtrShared pointer to compute hash code for
Returns
Hash code value

◆ GetTypeHash() [2/3]

template<typename ObjectType , ESPMode Mode>
uint32 GetTypeHash ( const TSharedRef< ObjectType, Mode > &  InSharedRef)

Computes a hash code for this object

Parameters
InSharedRefShared pointer to compute hash code for
Returns
Hash code value

◆ GetTypeHash() [3/3]

template<typename ObjectType , ESPMode Mode>
uint32 GetTypeHash ( const TWeakPtr< ObjectType, Mode > &  InWeakPtr)

Computes a hash code for this object

Parameters
InWeakPtrWeak pointer to compute hash code for
Returns
Hash code value

◆ MakeShareable() [1/2]

template<class ObjectType >
SharedPointerInternals::TRawPtrProxy< ObjectType > MakeShareable ( ObjectType *  InObject)
inline

MakeShareable utility function. Wrap object pointers with MakeShareable to allow them to be implicitly converted to shared pointers! This is useful in assignment operations, or when returning a shared pointer from a function.

◆ MakeShareable() [2/2]

template<class ObjectType , class DeleterType >
SharedPointerInternals::TRawPtrProxyWithDeleter< ObjectType, DeleterType > MakeShareable ( ObjectType *  InObject,
DeleterType &&  InDeleter 
)
inline

MakeShareable utility function. Wrap object pointers with MakeShareable to allow them to be implicitly converted to shared pointers! This is useful in assignment operations, or when returning a shared pointer from a function.

◆ MakeShared()

template<typename InObjectType , ESPMode InMode = ESPMode::ThreadSafe, typename... InArgTypes>
TSharedRef< InObjectType, InMode > MakeShared ( InArgTypes &&...  Args)
inline

MakeShared utility function. Allocates a new ObjectType and reference controller in a single memory block. Equivalent to std::make_shared.

NOTE: If the constructor is private/protected you will need to utilize a private token. Do not make SharedPointerInternals::TIntrusiveReferenceController a friend.

class FMyType
{
private:
    struct FPrivateToken { explicit FPrivateToken() = default; };

public:
    // This has an equivalent access level to a private constructor,
    // as only friends of FMyType will have access to FPrivateToken,
    // but MakeShared can legally call it since it's public.
    explicit FMyType(FPrivateToken, int32 Int, float Real, const TCHAR* String);
};

// Won't compile if the caller doesn't have access to FMyType::FPrivateToken
TSharedPtr<FMyType> Val = MakeShared<FMyType>(FMyType::FPrivateToken{}, 5, 3.14f, TEXT("Banana"));

◆ StaticCastSharedPtr()

template<class CastToType , class CastFromType , ESPMode Mode>
UE_FORCEINLINE_HINT TSharedPtr< CastToType, Mode > StaticCastSharedPtr ( TSharedPtr< CastFromType, Mode > const &  InSharedPtr)

Casts a shared pointer of one type to another type. (static_cast) Useful for down-casting.

Parameters
InSharedPtrThe shared pointer to cast

◆ StaticCastSharedRef()

template<class CastToType , class CastFromType , ESPMode Mode>
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef ( TSharedRef< CastFromType, Mode > const &  InSharedRef)

SharedPointer - Unreal smart pointer library

This is a smart pointer library consisting of shared references (TSharedRef), shared pointers (TSharedPtr), weak pointers (TWeakPtr) as well as related helper functions and classes. This implementation is modeled after the C++0x standard library's shared_ptr as well as Boost smart pointers.

Benefits of using shared references and pointers:

Clean syntax.  You can copy, dereference and compare shared pointers just like regular C++ pointers.
Prevents memory leaks.  Resources are destroyed automatically when there are no more shared references.
Weak referencing.  Weak pointers allow you to safely check when an object has been destroyed.
Thread safety.  Includes "thread safe" version that can be safely accessed from multiple threads.
Ubiquitous.  You can create shared pointers to virtually *any* type of object.
Runtime safety.  Shared references are never null and can always be dereferenced.
No reference cycles.  Use weak pointers to break reference cycles.
Confers intent.  You can easily tell an object *owner* from an *observer*.
Performance.  Shared pointers have minimal overhead.  All operations are constant-time.
Robust features.  Supports 'const', forward declarations to incomplete types, type-casting, etc.
Memory.  Only twice the size of a C++ pointer in 64-bit (plus a shared 16-byte reference controller.)

This library contains the following smart pointers:

TSharedRef - Non-nullable, reference counted non-intrusive authoritative smart pointer
TSharedPtr - Reference counted non-intrusive authoritative smart pointer
TWeakPtr - Reference counted non-intrusive weak pointer reference

Additionally, the following helper classes and functions are defined:

MakeShareable() - Used to initialize shared pointers from C++ pointers (enables implicit conversion)
MakeShared<T>(...) - Used to construct a T alongside its controller, saving an allocation.
TSharedFromThis - You can derive your own class from this to acquire a TSharedRef from "this"
StaticCastSharedRef() - Static cast utility function, typically used to downcast to a derived type. 
ConstCastSharedRef() - Converts a 'const' reference to 'mutable' smart reference
StaticCastSharedPtr() - Dynamic cast utility function, typically used to downcast to a derived type. 
ConstCastSharedPtr() - Converts a 'const' smart pointer to 'mutable' smart pointer
StaticCastWeakPtr() - Dynamic cast utility function, typically used to downcast to a derived type.
ConstCastWeakPtr() - Converts a 'const' smart pointer to 'mutable' smart pointer

Examples:

Tips:

  • Use TSharedRef instead of TSharedPtr whenever possible – it can never be nullptr!
  • You can call TSharedPtr::Reset() to release a reference to your object (and potentially deallocate)
  • Use the MakeShareable() helper function to implicitly convert to TSharedRefs or TSharedPtrs
  • Prefer MakeShared<T>(...) to MakeShareable(new T(...))
  • You can never reset a TSharedRef or assign it to nullptr, but you can assign it a new object
  • Shared pointers assume ownership of objects – no need to call delete yourself!
  • Usually you should "operator new" when passing a C++ pointer to a new shared pointer
  • Use TSharedRef or TSharedPtr when passing smart pointers as function parameters, not TWeakPtr
  • The "thread-safe" versions of smart pointers are a bit slower – only use them when needed
  • You can forward declare shared pointers to incomplete types, just how you'd expect to!
  • Shared pointers of compatible types will be converted implicitly (e.g. upcasting)
  • You can create a typedef to TSharedRef< MyClass > to make it easier to type
  • For best performance, minimize calls to TWeakPtr::Pin (or conversions to TSharedRef/TSharedPtr)
  • Your class can return itself as a shared reference if you derive from TSharedFromThis
  • To downcast a pointer to a derived object class, use the StaticCastSharedRef, StaticCastSharedRef or StaticCastWeakPtr functions
  • 'const' objects are fully supported with shared pointers!
  • You can make a 'const' pointer mutable using the ConstCastSharedRef, ConstCastSharedPtr or ConstCastWeakPtr functions

Limitations:

- Shared pointers are not compatible with Unreal objects (UObject classes)!
- Currently only types with that have regular destructors (no custom deleters)
- Dynamically-allocated arrays are not supported yet (e.g. MakeShareable( new int32[20] ))

Differences from other implementations (e.g. boost:shared_ptr, std::shared_ptr):

- Type names and method names are more consistent with Unreal's codebase
- You must use Pin() to convert weak pointers to shared pointers (no explicit constructor)
- Thread-safety features are optional instead of forced
- TSharedFromThis returns a shared *reference*, not a shared *pointer*
- Some features were omitted (e.g. use_count(), unique(), etc.)
- No exceptions are allowed (all related features have been omitted)
- Custom allocators and custom delete functions are not supported yet
- Our implementation supports non-nullable smart pointers (TSharedRef)
- Several other new features added, such as MakeShareable and nullptr assignment

Why did we write our own Unreal shared pointer instead of using available alternatives?

- std::shared_ptr (and even tr1::shared_ptr) is not yet available on all platforms
- Allows for a more consistent implementation on all compilers and platforms
- Can work seamlessly with other Unreal containers and types
- Better control over platform specifics, including threading and optimizations
- We want thread-safety features to be optional (for performance)
- We've added our own improvements (MakeShareable, assign to nullptr, etc.)
- Exceptions were not needed nor desired in our implementation
- We wanted more control over performance (inlining, memory, use of virtuals, etc.)
- Potentially easier to debug (liberal code comments, etc.)
- Prefer not to introduce new third party dependencies when not needed

Casts a shared reference of one type to another type. (static_cast) Useful for down-casting.

Parameters
InSharedRefThe shared reference to cast
Examples
/work/Engine/Source/Runtime/UMG/Public/Components/Widget.h.

◆ StaticCastWeakPtr()

template<class CastToType , class CastFromType , ESPMode Mode>
UE_FORCEINLINE_HINT TWeakPtr< CastToType, Mode > StaticCastWeakPtr ( TWeakPtr< CastFromType, Mode > const &  InWeakPtr)

Casts a weak pointer of one type to another type. (static_cast) Useful for down-casting.

Parameters
InWeakPtrThe weak pointer to cast

Variable Documentation

◆ TIsTSharedPtr_V

template<typename T >
constexpr bool TIsTSharedPtr_V = false
constexpr

Trait which determines whether or not a type is a TSharedPtr.

◆ TIsTSharedPtr_V< const TSharedPtr< ObjectType, InMode > >

template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedPtr_V< const TSharedPtr< ObjectType, InMode > > = true
constexpr

◆ TIsTSharedPtr_V< const volatile TSharedPtr< ObjectType, InMode > >

template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedPtr_V< const volatile TSharedPtr< ObjectType, InMode > > = true
constexpr

◆ TIsTSharedPtr_V< TSharedPtr< ObjectType, InMode > >

template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedPtr_V< TSharedPtr< ObjectType, InMode > > = true
constexpr

◆ TIsTSharedPtr_V< volatile TSharedPtr< ObjectType, InMode > >

template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedPtr_V< volatile TSharedPtr< ObjectType, InMode > > = true
constexpr

◆ TIsTSharedRef_V

template<typename T >
constexpr bool TIsTSharedRef_V = false
constexpr

Trait which determines whether or not a type is a TSharedRef.

◆ TIsTSharedRef_V< const TSharedRef< ObjectType, InMode > >

template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedRef_V< const TSharedRef< ObjectType, InMode > > = true
constexpr

◆ TIsTSharedRef_V< const volatile TSharedRef< ObjectType, InMode > >

template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedRef_V< const volatile TSharedRef< ObjectType, InMode > > = true
constexpr

◆ TIsTSharedRef_V< TSharedRef< ObjectType, InMode > >

template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedRef_V< TSharedRef< ObjectType, InMode > > = true
constexpr

◆ TIsTSharedRef_V< volatile TSharedRef< ObjectType, InMode > >

template<class ObjectType , ESPMode InMode>
constexpr bool TIsTSharedRef_V< volatile TSharedRef< ObjectType, InMode > > = true
constexpr

◆ TIsTWeakPtr_V

template<typename T >
constexpr bool TIsTWeakPtr_V = false
constexpr

Trait which determines whether or not a type is a TWeakPtr.

◆ TIsTWeakPtr_V< const TWeakPtr< ObjectType, InMode > >

template<class ObjectType , ESPMode InMode>
constexpr bool TIsTWeakPtr_V< const TWeakPtr< ObjectType, InMode > > = true
constexpr

◆ TIsTWeakPtr_V< const volatile TWeakPtr< ObjectType, InMode > >

template<class ObjectType , ESPMode InMode>
constexpr bool TIsTWeakPtr_V< const volatile TWeakPtr< ObjectType, InMode > > = true
constexpr

◆ TIsTWeakPtr_V< TWeakPtr< ObjectType, InMode > >

template<class ObjectType , ESPMode InMode>
constexpr bool TIsTWeakPtr_V< TWeakPtr< ObjectType, InMode > > = true
constexpr

◆ TIsTWeakPtr_V< volatile TWeakPtr< ObjectType, InMode > >

template<class ObjectType , ESPMode InMode>
constexpr bool TIsTWeakPtr_V< volatile TWeakPtr< ObjectType, InMode > > = true
constexpr