1) This is passing an array by reference. The actual type of the passed array is not really T, but is T[size].
2) So that you pass a reference to the array, rather than a pointer to an array. Since a pointer to an array is just a pointer, the size cannot be determined. But if you pass the whole array by reference, the size is part of the type and can thus be determined.
3)
T &v[5]
makes an array of 5 references.
T (&v)[5]
makes one reference to an array of 5 T's. The parenthesis here indicate the difference. The function just has the parameter as unnamed so it seems a little weird.
----------------------------
I remember touching on this topic in another thread recently....
http://cplusplus.com/forum/beginner/17680/
The idea is that T and T[size] are actually 2 different types. Think of it like this:
1 2 3 4 5
|
typedef int int4[4];
// with the above typedef, the below are the same:
int a[4];
int4 b;
|
If you know the type is 'int4' then you know that T=int and size=4. This is how the compiler fills in the size number.
The thing is... this ONLY works with arrays. It doesn't work with dynamically allocated buffers:
1 2
|
int* foo = new int[4];
int len = getLength(foo); // this doesn't work!
|
Or with arrays passed to a function by pointer -- EVEN IF you explicitly give a size in the function param:
1 2 3 4 5 6 7
|
void myFunc(int myarray[5])
{
getLength(myarray); // FAILS!
// although this would work if 'myarray' is passed by reference
// ie: int (&myarray)[5]
// but then this function suffers from the same problems as getLength
}
|
Basically.. the above getLength function isn't really very useful because it only gives you a size you'd already know.