What is the purpose of the fictitious addition in this & ldquo; Number of items & rdquo; Macro?

advertisements

Visual C++ 10 is shipped with stdlib.h that among other things contains this gem:

template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];

#define _countof(_Array) (sizeof(*__countof_helper(_Array)) + 0)

which uses a clever template trick to deduce array size and prevent pointers from being passed into __countof.

What's the purpose of + 0 in the macro definition? What problem does it solve?


Quoting STL from here

I made this change; I don't usually hack the CRT, but this one was trivial. The + 0 silences a spurious "warning C6260: sizeof * sizeof is usually wrong. Did you intend to use a character count or a byte count?" from /analyze when someone writes _countof(arr) * sizeof(T).