Error using std::initializer_list in a function C++

Hey for one of the functions in my class Mesh Generator I need to create a function that can take a variable number of parameters. After looking through documention it seems like the best way to do this for a variable number of parameters with the same type is using std::initializer_list. Note: I have included the #include as well. Here is the declaration of the function:

UFUNCTION()
		void MeshFromPoints(std::initializer_list<FMeshNode*> Points);

Now the code doesn’t have any errors when hovering over it and seems to recognise intializer_list but upon compiling I’m getting this error on that function declaration:

Cannot find class '', to resolve delegate 'initializer_list'

I’ve tried searching this error online but there seems to be literally no mention of it anywhere so I’m unsure of whether this is due to UE4 or purely a C++ problem.
If anyone knows any other way of getting around this and allowing for a variable number of parameters it would be greatly appreciated.

Hey Gama97-

If you want compile-time-sized arrays without dynamic memory allocation, you can try to use a fixed allocator with TArray. Then you can populate it as such:

 TArray<int32, TFixedAllocator<20>> MyArr;
 int32 Vals[] = { 1, 2, 3, 4, 5, 6 };
 MyArr.Append(Vals, ARRAY_COUNT(Vals));

… which is pretty much identical in execution terms to what an std::initializer_list-aware implementation would do.

Cheers

Sorry I’m not 100% clear on what you mean by this? For clarification my goal is to have a function call that takes a varied number of arguments. So for example I only have 1 function declaration like:

UFUNCTION()
         void MeshFromPoints(std::initializer_list<FMeshNode*> Points);

And then that means if I ever call MeshFromPoints I can insert as many points as necessary. In my case between 3 and 6 points. So for example:

MeshFromPoints(TopLeft, Top Right, BottomRight);

MeshFromPoints(BottomLeft, Top Left, Top Right, Centre Right, CentreBottom);

I think the C# equivalent is params

Looking through C++ documentation std::initializer_list seems to be somewhat equivalent and is used as so:

template <class T>
void func2( std::initializer_list<T> list )
{
    for( auto elem : list )
    {
        std::cout << elem << std::endl ;
    }
}

Thinking about it, it would probably make sense to make a temporary array of points that are sent through to the function but since the function call is in a for loop that will most likely loop around 1000x times I get the impression that this wouldn’t be very efficient?

I was about to make the suggestion of using an array myself. You can populate a TArray with the points necessary and pass that into your function. Unless you are manipulating physics assets or spawning new actors inside the function or doing other extensive processes, it shouldn’t cause a problem to use this method.

Sweet looks like it’s compiled fine using a TArray of points instead so I’m hoping it won’t be too inefficient and slow. Thanks for the help!

Should I make a new answer and accept it or just accept this one?

EDIT: Absolutely no noticeable difference in speed at all!