0

This is in reference to the first formal parameter in this question.

NOTE: T and arrsize are passed as template parameters here.

T (&arr)[arrsize] appears to be some sort of cast, but to what I am not sure. &arr is not a typename, it's just a formal parameter name with a reference symbol next to it. Why wouldn't one just put T* arr[]? Furthermore, I didn't think casting in a formal parameter list, or coercion, was possible in a language that implements function overloading. (i.e., introduces ambiguity in function calls). I suppose template functions introduce useful ambiguity, so is casting allowed in this context -- assuming two template functions don't have the same signature, barring the casting?

When this template is instantiated, what type does this argument become? Is it a pointer or a reference to a static array; simply T*; or something else?

Community
  • 1
  • 1
AlexMayle
  • 303
  • 2
  • 11

2 Answers2

2

T& arr[arrsize]: arr is an array (of size arraysize) of T&'s (Which is illegal in C++)

T (&arr)[arrsize]: arr is a reference to an array (of size arraysize) of T's

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Mattia F.
  • 1,720
  • 11
  • 21
2
T (&arr)[arrsize]

When used in a function parameter declares a variable named arr that is a reference to an array of type T with the size arrsize. This is a way to pass an array to a function and not have it decay into a pointer. You can use the array in the function just like you would in the scope you declared it in.

T& arr[arrsize]

Is an array of references which is not allowed per the standard. It should be a compiler error.

roalz
  • 2,699
  • 3
  • 25
  • 42
NathanOliver
  • 171,901
  • 28
  • 288
  • 402