struct size{
static const int cnt = N;
};
template <typename T, int N>
size <N> array_size(T (&a)[N]);
#define dimensionof(x) array_size(x).cnt
typedef unsigned char byte_t;
template <int N>
struct size_v1{
byte_t c[N];
};
template <typename T, int N>
size <N> array_size_v1(T (&a)[N]);
#define dimensionof_v1(x) sizeof(array_size(x).c)
template <typename T, int N>
byte_t (&dimen(T (&a)[N]) )[N];
#define dimmensionof_v2(x) sizeof(dimen(x))
更简单的实现
template <int N>
struct SIZE{
static const int cnt = N;
};
template <typename T, int N>
int arr_size(T (&arr)[N]){
/*
cout << sizeof(arr) / sizeof(T) << endl;//work well
struct SIZE <N> s;//also work well
cout << s.cnt << endl;
*/
return SIZE <N> ::cnt;
}