We know that arrays decay to pointers in function templates and to take an array type parameter we need to declare our function template with a reference-to-array:
template<class T, std::size_t N>
std::size_t number_of_elements(T (&ary)[N]) {
return N;
}
However, why do we not need to declare reference-to-array parameters in class templates? The code below shows this and compiles under C++11.
template<class T>
struct cls_number_of_elements {};
template<class T, std::size_t R>
struct cls_number_of_elements<T[R]> {
static const int N = R;
};
char ary[] = "12345";
auto const N = cls_number_of_elements<decltype(ary)>::N;
char ar2[N];