TArray initialization

Hey !

Is it possible to initialize a TArray at declaration like in C++11 with std::vector, std::array etc… ? I would like to avoid repeating multiple Add() to initialize a fixed size array :wink:

Something like:

TArray<int32> table = {4, 8, 15, 16, 23, 42};

Thanks in advance !

Hi,

We plan to add initialization_list support when all of supported compilers allow it. Currently, we are dependent on Visual Studio 2012, which does not.

We do not have a timescale for this, but for now you can do this to perform the equivalent operation:

int32 tableinit[] = {4, 8, 15, 16, 23, 42};
TArray<int32> table;
table.Append(tableinit, ARRAY_COUNT(tableinit));

Steve

1 Like

Could I just use the int32 array with ARRAY_COUNT() to get it’s size ? I didn’t know about this.

I don’t know what you mean by ‘just use’, but that’s what ARRAY_COUNT does: returns the number of elements in a C++ array. In this case, 6.

Steve

Well, as I didn’t know about ARRAY_COUNT before, my first idea was to use a TArray. Now it’s much easier to use for what I need, thank you !

Any update on this?

Hi,

This was added in UE 4.13:

“New: Added C++11 initializer list support to TArray and TSet.”

Steve

That’s awesome! Could you give me an example of how to set this up?
Preferably in a function call. :slight_smile:

EDIT: Seems like I got it to work, but an example would still be appreciated. :slight_smile:

Exactly like the OP posted:

TArray<int32> table = {4, 8, 15, 16, 23, 42};

Steve