Is it possible to avoid wrapping each constant with V and write something like?
This is C preprocessor, one way or the other you have to enumerate all possible iterations. You can write a foreach macro and apply it on each element.
I see no value in hiding a simple static char N[S][1 << 5] =
behind a macro. Just write static char CODES[LastExtensionError][1 << 5] =
. If you do want to repeat [1 << 5]
, consider making a structure.
#define FOREACH_1(f, _1) f(_1)
#define FOREACH_2(f, _1, ...) f(_1) FOREACH_1(f, __VA_ARGS__)
#define FOREACH_3(f, _1, ...) f(_1) FOREACH_2(f, __VA_ARGS__)
#define FOREACH_4(f, _1, ...) f(_1) FOREACH_3(f, __VA_ARGS__)
#define FOREACH_5(f, _1, ...) f(_1) FOREACH_4(f, __VA_ARGS__)
/* etc. add more cases as many as you need */
#define FOREACH_N(_5,_4,_3,_2,_1,N,...) FOREACH##N
#define FOREACH(f, ...) FOREACH_N(__VA_ARGS__,_5,_4,_3,_2_,_1)(f, __VA_ARGS__)
#define TBL_FOREACH_CB(a) [a] = #a,
#define TBL_INIT(...) { FOREACH(TBL_FOREACH_CB, __VA_ARGS__) }
static char CODES[LastExtensionError][1 << 5] = TBL_INIT(
Success,
BadRequest,
BadValue
)
You could use a library like BOOST_PP_FOR
or P99_FOR
if you do not want to write it yourself.
Also, it sounds odd that this is static char CODES[N][1<<5]
and not like static const char *const CODES[N]
. It's odd that values are mutable, and all strings have constant length, sounds like wasted memory.