参考自chromium的处理方法如下:
template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array)))
范例:
#include <iostream>
template <typename T, size_t N>
char (&ArraySizeHelper(T (&array)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array)))
int main()
{
int a[] = {1, 2, 3, 4, 5};
std::cout << "The count of a: " << arraysize(a) << std::endl;
return 0;
}
结果:
The count of a: 5
至于上述写法更详细的解释见
https://blogs.msdn.microsoft.com/the1/2004/05/07/how-would-you-get-the-count-of-an-array-in-c-2/。