RealDocs

TArray::InsertDefaulted_GetRef

function Core Since 4.20
#include "Containers/Array.h"
Access: public

Description

Inserts a single default-constructed element at Index and returns a reference to it. Equivalent to InsertDefaulted() followed by operator[], but avoids the second index lookup and is safe for non-trivial types.

Caveats & Gotchas

  • The returned reference is marked UE_LIFETIMEBOUND — any subsequent array mutation that triggers reallocation (Add, Reserve, more insertions) invalidates it. Do not hold this reference past the immediate initialization.
  • The default constructor is called before the reference is returned, so the element is fully constructed and safe to assign into.

Signature

ElementType& InsertDefaulted_GetRef(SizeType Index) UE_LIFETIMEBOUND

Parameters

Name Type Description Default
Index SizeType Position at which to insert the new element.

Return Type

ElementType&

Example

Insert and initialize in one expression C++
TArray<FTransform> Waypoints;
Waypoints.SetNum(2);

FTransform& NewWP = Waypoints.InsertDefaulted_GetRef(1);
NewWP.SetLocation(FVector(100.f, 200.f, 0.f));

Version History

Introduced in: 4.20

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.