Description
Appends an element to the end of the array and returns its index. Both copy and move overloads exist.
Signature
UE_FORCEINLINE_HINT SizeType Add(ElementType&& Item) Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| Item | ElementType&& | The element to append. Accepts both lvalue (copy) and rvalue (move) overloads. | — |
Return Type
int32 Caveats & Gotchas
- • Returns the index of the newly added element, not a bool. This is a common source of confusion when porting from std::vector::push_back.
- • May trigger a reallocation if the array exceeds its current capacity. Call Reserve() upfront when the final size is known to avoid repeated allocations.
- • For constructing an element in-place without a temporary, prefer Emplace() — it forwards constructor arguments directly and avoids a copy/move.
Example
Build a list of targets and use the returned index C++
TArray<AActor*> Targets;
Targets.Reserve(10);
for (AActor* Actor : AllActors)
{
if (IsValidTarget(Actor))
{
int32 Index = Targets.Add(Actor);
UE_LOG(LogTemp, Log, TEXT("Added target at index %d"), Index);
}
} See Also
Tags
Version History
Introduced in: unknown
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?