The trick here is that static_assert
can be used anywhere, even in a struct
definition. sizeof
return the size of the struct
, which !!
converts it to 1. char c
exists just so that the struct
is never empty. If the assertion fails, the code would not compile. If it succeeded, STATIC_ASSERT_EXPR
would return 1 (true
). This is better than the compiler flag because it does more than just warn, it causes compilation to fail.
0[ARRAY]
instead of ARRAY[0]
because in C++, trying to use ARRAY_SIZEARRAY_CARDINALITY
on an object of a class
for which operator[]
has been overloaded would result in a compilation error, which is what one would want.
Now the only thing GNU C specific thing in your code is the statement expression, for which I know of no ISO C version. I believe the C committee is working on something similar.