Download

C++ can we pass arrays, or other collections on function line?

I’m just trying to pass an array defined on the function line to the constructor for my struct. I’m generating the code based on XML, so it would just make it simpler to generate. I was just wondering if C++ has syntax similar to other languages for doing this. I found some information online that doesn’t work, guess the proposal never went through for the updated standards.

Here’s what I have:



DialogueNodes(FText dialogueFragment, TArray<FName> nextNode)
          : DialogueFragment(dialogueFragment)
          {
	          NextNode.Append(nextNode, sizeof(nextNode)); // Doesn't work for getting sizeof(array) across scope
          }

DialogueNodes(
LOCTEXT("Dialogue26","You about ready to go?"), [TEXT("0x01000003000001D7"), TEXT("0x01000003000001DF")]))

I tried using FName nextnode] and TArray<FName> nextNode, without any success. I also tried surrounding the array memberes with ] and {}. Is there a macro, or something specific to unreal I can use? It looks like std::array<string, count> nextNode would have worked with brackets around members, but I’m pretty sure it’s a bad idea to use stl with Unreal, and I don’t know the size of the array before hand.

I’m confused, what exactly are you trying to accomplish here?

You claim that there is a problem passing TArray as a function parameter, however that works perfectly fine.

It seems that your problem stems from the sizeof(nextNode), which tells me that you’re trying to append raw data from one array to another. Try using the alternative:


NextNode.Append(nextNode);

As long as the Arrays are of the same type (TArray<FName>::Append(TArray<FName>)), it should just work.

I’m trying to not have an explicit TArray declared before calling the DialogueNodes constructor. Like in Ruby where [1, 2, 3, 4, 5] would get picked up by splat character in a method argument, and interpreted as an array, or this C++ examplefrom stl with {} around the parameters. I believe that works for all of stl, either since C++11 or 14. Was just wondering if there is an equivalent.

This works with vectors in C++14 (Also works on C++11), at least on Clang.



void test(std::vector<int> test)
	{
		for (const auto t : test)
		{
			std::cout << t << std::endl;
		}
	}

int main()
{
     test({1,2,3,4});
}


I checked the docs and there is no constructor to help with that case. You could create a variadic function that builds the TArray from the parameters and returns it. Something like:



template <class T>
TArray<T> MakeTArray(T first...) {
}


Alright, I’ll look into Variadic functions. If I wanted to hack {} initialization in, would that be possible in theory, or is that something that can only be done through the compiler?

I don’t get it; you try to append a zero sized array there.
If you gonna declare as a parameter at least give it a value first.

I’m having a bit of a hard time getting variadic functions to work correctly. All of the examples I’ve found use some stl for this, std::initializer_list.

This is what I was able to get compiling:



inline TArray<FName> pass(FName args)
{
	TArray<FName> temp;
	temp.Emplace(args);
	return temp;
}
	
template<typename... Args> 
inline TArray<FName> pass(Args... args)
{
	TArray<FName> temp;
	temp.Append(pass(args...));
	return temp;
}

Banter::Banter()
{
     	TArray<FName> temp;
	temp.Append(pass(TEXT("Test"), TEXT("Test2")));
}



I believe it just causes infinite recursion when run, since the editor crashes when opening the project. I tried playing around with having a template on the base case, but was just getting compiler errors. Have any suggestions on how to implement this correctly?

Here’s a couple of links that may help:

http://en.cppreference.com/w/cpp/utility/variadic

Edit: 4.13 now supports what you want (I think):

Yeah, that’s it. I’m going to have to see if that can be backported. Stuck on 4.12 till NVidia and Algorithmic release their 4.13 branch.

Those all use things from std. Is it safe to use any std with Unreal? As long as it builds, I’m fine with it as will be using 4.13 by releasing time. Substance is already up to 4.13, we’re just waiting on NVidia.