Sure, though templates are definitely not beginner level stuff, so I wouldn’t worry about it if you’re still confused after.
That is correct, yes. That is the weird looking syntax to pass an array by reference. It uses a template parameter so it can work with any sized array that’s known at compile time
template<SIZE_T N>
void Something(const int32 (&A)[N])
{
// use A
}
// using that template:
int32 Test1[3];
int32 Test2[5];
Something(Test1); // Deduces N to be 3. Instantiates the template Something<3>();
Something(Test1); // Deduces N to be 5. Instantiates the template Something<5>();
A string literal is just a character array. So
TEXT("Hello")
Would be of type
const TCHAR[6]
(6 due to the implicit null terminator.)
So hopefully putting all of that together explains the first parameter. It’s just passing the format string by reference.
That is a template parameter pack. It’s for a variable number of template parameters. Consider the following
PrintLine(TEXT("I'm %s, and I'm %i years old", TEXT("Dan"), 29);
PrintLine(TEXT("%f is a cool number, %s"), 3.14f, TEXT("Bob");
PrintLine(TEXT("The answer to everything is %i ", 42);
Here PrintLine is called in 3 different ways
- With 2 format arguments, a string literal and an int.
- With 2 format arguments, a float and a string.
- With 1 format argument, an int.
And a variadic template is how you are able to express that.
The above would instantiate the following templates
// Calling code
PrintLine<30, const TCHAR*, int>(TEXT("I'm %s, and I'm %i years old", TEXT("Dan"), 29);
// Generated function
void PrintLine(const TCHAR (&Fmt)[30], const TCHAR* Arg1, int32 Arg2) const
{
PrintLine(FString::Printf(Fmt, Arg1, Arg2));
}
// Calling code
PrintLine<24, float, const TCHAR*>(TEXT("%f is a cool number, %s"), 3.14f, TEXT("Bob");
// Generated function
void PrintLine(const TCHAR (&Fmt)[24], float Arg1, const TCHAR* Arg2) const
{
PrintLine(FString::Printf(Fmt, Arg1, Arg2));
}
// Calling code
PrintLine<31, int>(TEXT("The answer to everything is %i ", 42);
// Generated function
void PrintLine(const TCHAR (&Fmt)[31], int32 Arg1) const
{
PrintLine(FString::Printf(Fmt, Arg1));
}
(It deduced const TCHAR* as arrays decay into pointers, which is why you need that weird syntax to pass by reference)
Because printf is a C function that only works with primitive types. FString::Printf
uses a printf function to create an FString.