Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this C++ code mean?

Tags:

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...

like image 467
Luchian Grigore Avatar asked Nov 01 '11 13:11

Luchian Grigore


3 Answers

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))) 
like image 76
Maxim Egorushkin Avatar answered Oct 03 '22 13:10

Maxim Egorushkin


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.

like image 33
Flexo Avatar answered Oct 03 '22 15:10

Flexo


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.

like image 42
MäxL Avatar answered Oct 03 '22 15:10

MäxL