I have come across the following stripped down code that passes an array into a constructor:
struct FF
{
template <int N> FF(int(&a)[N]);
private:
void* arena_;
};
template <int N>
inline FF::FF(int(&a)[N])
: arena_(a)
{
/*empty*/
}
This can be called as follows:
int intArr[5];
FF ff(intArr);
Now, in int(&a)[N]
, clearly the template argument N
is going to be taken from the size of the array, 5 in this case, but I don't understand the int(&a)
part. Whenever I have passed an array before, I have always passed it to a function expecting a pointer (since passing an array is the same as passing the address of the first element of that array, so we effectively assign &array to *). But here we seem to be passing the address of the array to a constructor expecting what looks like a reference?
I then added a second constructor:
FF(int* pArr) {}
Now, the templated constructor doesn't get called at all, this one does!
Can someone please explain this strange templated form of the constructor and what it is saying, and then why when I add a constructor taking a pointer, the templated version doesn't get called at all?