array_length: Make usable as a constant expression

Do not use a statement expression in array_length, so that
array_length can be used at file scope and as a constant expression.
Instead, put the _Static_assert into a struct (as a declaration),
and nest this in the expression using a sizeof expression.
This commit is contained in:
Florian Weimer 2019-02-07 09:03:02 +01:00
parent 94b63e6620
commit 8311c83f91
2 changed files with 12 additions and 6 deletions

View File

@ -1,3 +1,9 @@
2019-02-07 Florian Weimer <fweimer@redhat.com>
* include/array_length.h (array_length): Do not use a statement
expression and _Static_assert, so that array_length can be used at
file scope and as a constant expression.
2019-02-07 Florian Weimer <fweimer@redhat.com> 2019-02-07 Florian Weimer <fweimer@redhat.com>
* support/xdlfcn.h (xdlmopen): Declare. * support/xdlfcn.h (xdlmopen): Declare.

View File

@ -22,12 +22,12 @@
/* array_length (VAR) is the number of elements in the array VAR. VAR /* array_length (VAR) is the number of elements in the array VAR. VAR
must evaluate to an array, not a pointer. */ must evaluate to an array, not a pointer. */
#define array_length(var) \ #define array_length(var) \
__extension__ ({ \ (sizeof (var) / sizeof ((var)[0]) \
_Static_assert (!__builtin_types_compatible_p \ + 0 * sizeof (struct { \
(__typeof (var), __typeof (&(var)[0])), \ _Static_assert (!__builtin_types_compatible_p \
"argument must be an array"); \ (__typeof (var), __typeof (&(var)[0])), \
sizeof (var) / sizeof ((var)[0]); \ "argument must be an array"); \
}) }))
/* array_end (VAR) is a pointer one past the end of the array VAR. /* array_end (VAR) is a pointer one past the end of the array VAR.
VAR must evaluate to an array, not a pointer. */ VAR must evaluate to an array, not a pointer. */