22

The following code returns the size of a stack-allocated array:

template<typename T, int size>
int siz(T (&) [size])
{
    return size;
}

but I can't wrap my head around the syntax. Especially the T (&) [size] part...

3

3 Answers 3

24

but I can't wrap my head around the syntax. Especially the T (&) [size] part...

That part is a reference to an array. There is the "right-left rule" for deciphering any C and C++ declarations.

Because function templates deduce template argument types from the supplied function arguments what this function template does is deduce the type and element count of an array and return the count.

Functions can't accept array types by value, rather only by pointer or reference. The reference is used to avoid the implicit conversion of an array to the pointer to its first element (aka, array decay):

void foo(int*);

int x[10];
int* p = x; // array decay
foo(x);     // array decay again

Array decay destroys the original type of the array and hence the size of it gets lost.

Note, that because it is a function call in C++03 the return value is not a compile time constant (i.e. the return value can't be used as a template argument). In C++11 the function can be marked with constexpr to return a compile time constant:

template<typename T, size_t size>
constexpr size_t siz(T(&)[size]) { return size; }

To get the array element count as a compile time constant in C++03 a slightly different form may be used:

template<class T, size_t size>
char(&siz(T(&)[size]))[size]; // no definition required

int main()
{
    int x[10];
    cout << sizeof siz(x) << '\n';
    double y[sizeof siz(x)]; // use as a compile time constant 10
}

In the above it declares a function template with the same reference-to-an-array argument, but with the return value type of char(&)[size] (this is where the "right-left rule" can be appreciated). Note that the function call never happens at run-time, this is why the definition of function template siz is unnecessary. sizeof siz(x) is basically saying "what would be the size of the return value if siz(x) were called".

The old C/C++ way of getting the element count of an array as a compile time constant is:

#define SIZ(arr) (sizeof(arr) / sizeof(*(arr)))
6
  • are the parens around the & necessary?
    – Jason S
    Nov 1, 2011 at 13:25
  • @JasonS - the parens are required to avoid looking like an array of references to T
    – Flexo
    Nov 1, 2011 at 13:38
  • ah, ok... and (T&)[size] won't work either? (sorry, don't have a C compiler up and running)
    – Jason S
    Nov 1, 2011 at 14:10
  • 1
    @JasonS No, that's the same as just T&. The parenthesis make it clear that the & doesn't refer to the type the elements of the array are made from. (A C compiler wouldn't do much good for testing it out though - there's no references in C!)
    – Flexo
    Nov 1, 2011 at 14:15
  • 1
    @awoodland: AH! The light finally went on. so reference to array of T rather than array of references.
    – Jason S
    Nov 2, 2011 at 17:04
7

T (&) [size] is a reference to an array. It needs to be a reference because the following program is not legal:

#include <iostream>

int sz(int *) { std::cout << "wtf?" << std::endl; return 0; }


int sz(int [4]) { std::cout << "4" << std::endl; return 0; }

int main() {
  int test[4];
  sz(test);
}

This program fails to compile with:

test.cc: In function ‘int sz(int*)’:
test.cc:6:5: error: redefinition of ‘int sz(int*)’
test.cc:3:5: error: ‘int sz(int*)’ previously defined here

because int sz(int [4]) is identical to int sz(int *).

The parenthesis are required to disambiguate here because T& [size] looks like an array of references which is otherwise illegal.

Normally if the parameter wasn't anonymous you would write:

template<typename T, int size>
int sz(T (&arr) [size])

To give the array the name arr. In this instance though all your example code cared about was the deduced size and hence the anonymous argument avoids warnings about unused arguments.

4

It´s an function which becomes a typename (templates can be used with different typenames) and a size from the outside. It then returns this size.

Stack functions often use size, which is an integer number which shows you the size of the stack-size you request with this function. The & tests just which size of stack T is meant.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.