RealDocs

TSubclassOf::UE_REQUIRES (constrained conversion constructor)

function CoreUObject Since 5.0
#include "Templates/SubclassOf.h"
Access: public Specifiers: inline

Description

Constrained conversion constructor that allows a TSubclassOf<Derived> to be implicitly converted to a TSubclassOf<Base> only when Derived* is convertible to Base*. This enforces type-safe upcasting at compile time.

Caveats & Gotchas

  • Only upcast conversions (derived-to-base) are permitted by this constructor — there is no corresponding downcasting constructor; to narrow a TSubclassOf<Base> to TSubclassOf<Derived> you must manually check IsChildOf and assign a UClass* directly.
  • This constraint replaced the older unconstrained conversion in UE 5.0; code that relied on implicit cross-hierarchy class conversions will fail to compile without explicit casts.

Signature

template < typename OtherT UE_REQUIRES(std::is_convertible_v<OtherT*, T*>) > [[nodiscard]] inline TSubclassOf(const TSubclassOf<OtherT>& Other)

Parameters

Name Type Description Default
Other const TSubclassOf<OtherT>& The source TSubclassOf whose type OtherT must be a subtype of T.

Example

Implicit upcast between TSubclassOf types C++
TSubclassOf<AMyEnemy> EnemyClass = AMyBossEnemy::StaticClass();
// Safe: AMyBossEnemy* is convertible to APawn*
TSubclassOf<APawn> PawnClass = EnemyClass; // constrained constructor selected

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.