3

Possible Duplicate:
Can someone explain this template code that gives me the size of an array?

Hi, I am looking at the answer posted here:

Computing length of array

Copied here:

template <typename T, std::size_t N>
std::size_t size(T (&)[N])
{
    return N;
}

Can some one pls explain me the significance of (&) ?

Community
  • 1
  • 1
Kiran
  • 5,478
  • 13
  • 56
  • 84
  • This is a different question. – Robert Harvey Feb 17 '11 at 19:38
  • 1
    @RobertHarvey: No, they are the same, but worded differently. This specifically calls out the &, while the other asks about the dimension, but the dimension has meaning *solely* because of the reference. Note "What happens with the array when I pass it" (on the other) is directly answered by "'the significance of the &' (from here) means it is passed by reference". – Fred Nurk Feb 17 '11 at 19:42
  • Let's keep it open. The answers on the two questions are substantially different, and the close reason says *"Exact* duplicate." – Robert Harvey Feb 17 '11 at 19:48
  • @RobertHarvey: I don't see how they could be closer duplicates without one copying the literal text from the other. But, whatever, they'll now be linked from the automatically inserted comment above. – Fred Nurk Feb 17 '11 at 19:52
  • @Fred: Sorry, looking in the wrong place. – Robert Harvey Feb 17 '11 at 19:54

2 Answers2

7

The & says the array is passed by reference. That prevents the type from decaying to pointer. Without passing by reference you would get that decay, and no information about the array size.

Non-template example:

void foo( int (&a)[5] )
{
    // whatever
}

int main()
{
    int x[5];
    int y[6];

    foo( x );           // ok
    //foo( y );         // !incompatible type
}

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Thanks. Agreed, I am more confused with the parentheses around the &. Why are they needed? I tested and it fails compilation without them.. – Kiran Feb 17 '11 at 19:23
  • Better explanation. So deleted mine! – Nawaz Feb 17 '11 at 19:24
  • For clarity, without passing by reference the given dimension is just a hint and not actually part of the type of the parameter. That is, void foo(int a[5]) is *exactly* the same as void foo(int *a). When calling foo(x) for this non-reference form, you get the array-to-pointer decay mentioned. – Fred Nurk Feb 17 '11 at 19:26
  • 1
    @Kiran: Without the parenthesis you would be trying to declare an array of references, which is not permitted: you can't have an array of references. – Cheers and hth. - Alf Feb 17 '11 at 19:28
2

The & means that the array is to be passed by reference. Arrays can't be passed by value, because in this case they decay to a pointer to the first item. If that happens, the size information associated with the array type is lost.

It is in parenthesis, because otherwise that would mean the function accepts a pointer to reference. Although such a thing is not legal in C++, the syntax is consistent with how you declare a pointer to array.

int (*arr)[3]; // pointer to array of 3 ints
int* p_arr[3]; // array of pointers

int (&arr)[3]; // reference of array of 3 ints
int& r_arr[3]; //array of references (not legal in C++)
UncleBens
  • 40,819
  • 6
  • 57
  • 90