I’ve found this article that brings up the following template and a macro for getting array size:
template<typename Type, size_t Size>
char ( &ArraySizeHelper(Type( &Array )[Size]) )[Size];
#define _countof(Array) sizeof(ArraySizeHelper(Array))
and I find the following part totally unclear. sizeof is applied to a function declaration. I’d expect the result to be “size of function pointer”. Why does it obtain “size of return value” instead?
It’s not
sizeof ArraySizeHelper(which would be illegal – can’t takesizeofa function), norsizeof &ArraySizeHelper– not even implicitly as implicit conversion from function to pointer-to-function is explicitly disallowed by the Standard, for C++0x see 5.3.3). Rather, it’ssizeof ArraySizeHelper(Array)which is equivalent tosizeofthe value that the function call returns, i.e.sizeof char[Size]henceSize.