There are several good reasons to prefer
#include <cstdlib>
template<typename T, std::size_t N>
constexpr std::size_t ARRAY_COUNT_FUNC(T (&arr)[N]) { return N; }
rather than
#define ARRAY_COUNT_MACRO(arr) (sizeof(arr)/sizeof(*arr))
One important difference is that when a pointer (not an array) is passed to ARRAY_COUNT_MACRO, it silently returns an unhelpful answer, but passing the same argument to ARRAY_COUNT_FUNC will cause a compiler error pointing out the mistake.
But the macro does have one advantage: its argument is unevaluated.
#include <utility>
struct S {
int member_array[5];
};
// OK:
std::size_t count1 = ARRAY_COUNT_MACRO(std::declval<S&>().member_array);
// ERROR: std::declval is odr-used!
std::size_t count2 = ARRAY_COUNT_FUNC(std::declval<S&>().member_array);
Is there another approach with the benefits of both together? I. e., something that causes a compile error if the argument is not an array, and does not odr-use its argument.
Shamelessly ripped off from the Chromium project, as described here.