RealDocs

TWeakPtr::UE_REQUIRES (constrained conversion constructor)

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

Description

Constrained conversion constructor that copies a TWeakPtr<OtherType> into a TWeakPtr<ObjectType> only when OtherType* is convertible to ObjectType*. Enables type-safe derived-to-base weak pointer conversions.

Caveats & Gotchas

  • The ESPMode template parameter (Mode) must match between source and destination — you cannot convert between a ThreadSafe and a NotThreadSafe TWeakPtr using this constructor.
  • Like all weak pointer copies, this constructor only copies the weak reference count controller and the raw object pointer; it does not extend the object's lifetime or Pin() it.

Signature

template < typename OtherType UE_REQUIRES(std::is_convertible_v<OtherType*, ObjectType*>) > inline TWeakPtr( TWeakPtr< OtherType, Mode > const& InWeakPtr )

Parameters

Name Type Description Default
InWeakPtr TWeakPtr< OtherType, Mode > const& Source weak pointer whose OtherType must be implicitly convertible to ObjectType.

Example

Storing a derived weak ptr as a base weak ptr C++
TSharedPtr<FMyDerivedClass> Derived = MakeShared<FMyDerivedClass>();
TWeakPtr<FMyDerivedClass> WeakDerived = Derived;
// Upcast to base via constrained constructor
TWeakPtr<FMyBaseClass> WeakBase = WeakDerived;
if (TSharedPtr<FMyBaseClass> Pinned = WeakBase.Pin())
{
    Pinned->DoWork();
}

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.