RealDocs

TSharedRef::UE_REQUIRES

function Core Since 5.0
#include "Templates/SharedPointer.h"
Access: public Specifiers: inlineexplicit

Description

Template constraint on TSharedRef's converting constructors that ensures only type-compatible (upcast-safe) pointer types can implicitly construct a TSharedRef<ObjectType>.

Caveats & Gotchas

  • Unlike TSharedPtr, TSharedRef has no default constructor. Every constructor must receive a non-null pointer, and the UE_REQUIRES constraint additionally enforces type safety. Passing a pointer that fails the constraint produces a hard compile error.
  • The constraint does not apply to the private constructors used by StaticCastSharedRef and ConstCastSharedRef, which use explicit cast tags to bypass it.
  • TSharedRef enforces non-null at runtime via a check(InObject != nullptr) call inside Init(), in addition to the compile-time type constraint. Both checks must pass.

Signature

UE_REQUIRES(std::is_convertible_v<OtherType*, ObjectType*>)

Parameters

Name Type Description Default
OtherType template parameter Source pointer type. Must be implicitly convertible to ObjectType* for the constructor to be considered.

Example

Upcast is implicit; downcast requires StaticCastSharedRef C++
TSharedRef<FDerived> DerivedRef = MakeShared<FDerived>();

// OK: FDerived* is convertible to FBase*
TSharedRef<FBase> BaseRef = DerivedRef;

// ERROR — violates UE_REQUIRES:
// TSharedRef<FDerived> BackRef = BaseRef;

// Correct downcast:
TSharedRef<FDerived> BackRef = StaticCastSharedRef<FDerived>(BaseRef);

Version History

Introduced in: 5.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.