Quantcast
Channel: User YSC - Stack Overflow
Viewing all articles
Browse latest Browse all 52

How to get an array size at compile time?

$
0
0

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 1024int some_array[ARRAY_SIZE];

or

#define countof(ARRAY) (sizeof(ARRAY)/sizeof(ARRAY[0]))

In C++, is it possible to define something more idiomatic?


Viewing all articles
Browse latest Browse all 52

Latest Images

Trending Articles





Latest Images