When defining either a C-style array or a C++11 array, one often need to get a compile-time constant to express the size of such array. In C, a macro is used to do such a thing without relying on variable-length array (as it wasn't standard in C99):
#define ARRAY_SIZE 1024
int some_array[ARRAY_SIZE];
or
#define countof(ARRAY) (sizeof(ARRAY)/sizeof(ARRAY[0]))
In C++, is it possible to define something more idiomatic?
If your standard-library is C++17-compatible, just use std::size(xyz)
.
If your standard-library doesn't already provide that, it's easy to implement yourself, the constexpr
-specifier is C++11.