Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

templates - What does this C++ code mean?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) << '
';
    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)))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...