C++利用函数模板得到数组的长度
#include<iostream>
template <typename T, int N>
char (&ArraySizeHelper(T (&arr)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array)))
template <typename T, int N>
int ArraySize (T (&arr)[N])
{
return N;
}
int main()
{
int arr[10];
std::cout << ArraySize(arr) << "," << arraysize(arr) << std::endl;
double arr2[20];
std::cout << ArraySize(arr2) << "," << arraysize(arr2) << std::endl;
return 0;
}