Give TArray a size from an object's initializer list?

This idea works well with std::vector, allowing the initialization of vectors as member variables within the class’ initializer list. Obviously that’s preferable to first creating the object and the vector, then in the constructor giving it a size. Why write

int a;
a = 5;

When you could just use

int a = 5;

Same deal here - we should be able to do something with most dynamic arrays, right?

struct FObjectCollection
{
     FObjectCollection(int a_numObjects)
        : m_IDs(std::vector<int>(a_numObjects))
     {

     }
        
     std::vector<int> m_IDs;
};

This works. But TArray doesn’t support this.

struct FObjectCollection
{
	FObjectCollection(int32 a_numObjects)
		: m_IDs(TArray<int32>(a_numObjects))
	{

	}

	TArray<int32> m_IDs;
};

It would be fantastic if it could!

You could do this

FObjectCollection(int32 a_numObjects)
{
     m_IDs.AddZeroed(a_numObjects);
}

It’s not in the initializer list but it gets the job done.

Totally, and that’s what I’ve done before. But like the opening post describes, that’s an ‘int a; a = 5;’ approach. It works fine, but we could so easily be better-off if Epic allowed us to follow the std::vector initialization style. I’m sure it’s not as trivial as I imagine, but not that crazy.

EDIT: I realise that this seems trivial. And really, it probably is. But it’d be a nice little change for usability :slight_smile: