vendredi 23 mars 2018

How to get an array size at compile time?

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?

Aucun commentaire:

Enregistrer un commentaire