The original code might have built what today is provided by std::rank, see section "Possible implementation"; for not having to inherit from integral constant (that wouldn't have existed in those days), it could be modified to:
template<typename T>
struct rank { static size_t const value = 0U; };
template<typename T>
struct rank<T[]> { static size_t const value = rank<T>::value + 1; };
template<class T, std::size_t N>
struct rank<T[N]> { static size_t const value = rank<T>::value + 1; };
The code then would have used this template class in the template function (the template class might have been local classes in the function as well):
template <typename T>
size_t dimOfArray(T const& array)
{
return /*std::*/rank<T>::value;
}
With modern C++ best declare this function constexpr
. If you are rather after getting the size of one specific dimension, you could use this:
template <typename T, size_t N>
size_t dimOfArray(T const (&array)[N]) constexpr
{
return N;
}
Again, today you might want to have it constexpr
(as shown).
The latter (size of a specific dimension) is what you could achieve with sizeof(a)/sizeof(*a)
"tricks" as well (instead of the much simpler returning of N
), I don't see, though, how you'd want to get the number of dimensions with.
a
is a multidimensional array, which dimension shouldDimOfArray
return? – Supersaturatestd::rank
(orstd::rank_v
). – Coronel