RealDocs

TSharedPtr::UE_REQUIRES

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

Description

Template constraint applied to TSharedPtr's converting constructors and assignment operators. Ensures at compile time that the source pointer type is implicitly convertible to the target type, preventing silent type-unsafe conversions.

Caveats & Gotchas

  • UE_REQUIRES is an Unreal macro that expands to a C++20 requires clause or a std::enable_if fallback depending on the compiler. It is not a callable function — it appears in the template parameter list and produces a hard compile error if not satisfied.
  • For downcasting (base* to derived*) use StaticCastSharedPtr() or ConstCastSharedPtr(). These bypass the UE_REQUIRES constraint via explicit cast constructors that are intentionally not constrained.
  • The constraint applies to the converting constructors from TSharedPtr<OtherType>, TSharedRef<OtherType>, and raw pointer constructors, but not to the StaticCastTag/ConstCastTag constructors used by the cast helpers.

Signature

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

Parameters

Name Type Description Default
OtherType template parameter The type of the incoming pointer or shared pointer. The constraint enforces that OtherType* is implicitly convertible to ObjectType*, enabling safe upcasting.

Example

Implicit upcast allowed; downcast requires StaticCastSharedPtr C++
TSharedPtr<FDerived> DerivedPtr = MakeShared<FDerived>();

// OK: FDerived* is convertible to FBase* (upcast)
TSharedPtr<FBase> BasePtr = DerivedPtr;

// ERROR: FBase* is NOT convertible to FDerived* — UE_REQUIRES fails
// TSharedPtr<FDerived> Back = BasePtr;

// Correct downcast:
TSharedPtr<FDerived> Back = StaticCastSharedPtr<FDerived>(BasePtr);

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.