It's an overly complicated declaration. In modern C++, you can do just this:
template<typename T, size_t SizeOfArray>
constexpr size_t countof(T (&array)[SizeOfArray]) { return SizeOfArray; }
The crux of the solution is T (&array)[SizeOfArray]
: it's the syntax you need to pass a reference to an array. T
and SizeOfArray
are inferred by the compiler, so it'll have valid values for any array you throw at it.
In your helper case, the programmer probably didn't have access to constexpr
but still wanted to make sure that the expression was evaluated at compile-time rather than at runtime. It's confusing because the C++ function declaration syntax is confusing: it declares a function that accepts an array of any type and length and returns an array of characters of the same length. It then uses sizeof
to find the length of the character array and returns that.