Is there the equivalence of std::array::swap sor TArray

I’m looking to do a container swap between 2 TArrays, but I can only find an element swap in TArray.

Hey sviltofsky_pdg -

There is not a built in function for swapping the contents of one TArray with another TArray. The easiest way to add this behavior would be to use a for loop that will iterate through the arrays and a placeholder variable that will temporarily hold the value of one element while you switch the value of the other array’s element into the first array.


Doug Wilson

Yeah the whole goal here was to do the swap in constant time without moving memory around.


There is a Swap function in engine core.

template <typename T>
inline typename TEnableIf<!TUseBitwiseSwap<T>::Value>::Type Swap(T& A, T& B)
	T Temp = MoveTemp(A);
	A = MoveTemp(B);
	B = MoveTemp(Temp);

Since TArray is MoveAssignable, the Swap function is exactly what you need.
Use it like this:

TArray<int> first;
TArray<int> second;

Swap(first, second);