G_STATIC_ASSERT_EXPR() unusable in array initialisation due to , operator
Submitted by Dr. David Alan Gilbert
Link to original bug (#772582)
Description
I'm told that the C11 standard doesn't allow the ',' operator to be regarded as constant, and thus the G_STATIC_ASSERT_EXPR is unusable in an array initialisation.
eg: #include <glib.h> static const int x[]={(G_STATIC_ASSERT_EXPR(1==1),4)};
int main(int argc, char* argv[]) { return x[0]; }
t.c:2:23: error: initializer element is not constant static const int x[]={(G_STATIC_ASSERT_EXPR(1==1),4)}; ^ t.c:2:23: note: (near initialization for ‘x[0]’)
(that's with gcc 4.8.5 and 6.2.1)
I'm told the C standard says: "Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within a subexpression that is not evaluated."
which is a pain, since https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html#G-STATIC-ASSERT-EXPR:CAPS recommends the use exactly as I've got it.
Note that I find the following apparently works:
#define BUILD_BUG_EXPR(x) (sizeof(int [(x)?1:-1])-sizeof(int [1]))
static const int y[]={0+BUILD_BUG_EXPR(1==1)};
(although whether the C standard requires it to work is a different matter).
Version: 2.50.x