Believe it or not, I want to use static_assert
in a macro that expands to a designated initializer:
#define INIT(N) \
/* static_assert((N) < 42, "too large"), */ \
[(N)] = (N)
int array[99] = { INIT(1), INIT(2), INIT(42) };
I want an error from INIT(42)
, but uncommenting the static_assert
is a syntax error. AFAIK static_assert
is syntactically a declaration. How can I use it in this example?
... I'm not sure myself how I ended up with that abomination. But hey, it works (for
N > 0
) !0
-friendly version (just adding then subtracting1
so the array has a positive size):