August 2012 Archives

ArraySizeHelper

| No Comments | No TrackBacks

在chromium的src/base/basictype.h中发现一段神奇的marco

template<typename T, size_t N>
char (&ArraySizeHelper(T (&array)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array)))


C++11版本 :
template<typename T, size_t N>
auto ArraySizeHelper(T (&array)[N]) -> char(&)[N];

解释:

The function template is named ArraySizeHelper, for a function that takes one argument, a reference to a T [N], and returns a reference to a char [N].

The macro passes your object (let's say it's X obj[M]) as the argument. The compiler infers that T == X and N == M. So it declares a function with a return type of char (&)[M]. The macro then wraps this return value with sizeof, so it's really doing sizeof(char [M]), which is M.

If you give it a non-array type (e.g. a T *), then the template parameter inference will fail.

As @Alf points out below, the advantage of this hybrid template-macro system over the alternative template-only approach is that this gives you a compile-time constant.

http://stackoverflow.com/questions/6376000/how-does-this-array-size-template-work