_Static_assert / verify for internal use in glibc
Paul Eggert
eggert@cs.ucla.edu
Tue Dec 3 21:05:00 GMT 2013
[Changing the subject line]
On 12/03/2013 09:53 AM, Joseph S. Myers wrote:
> _Static_assert was added in 4.6, so using it would require
> increasing the minimum GCC version for building glibc.
Instead of changing the minimum GCC version, we could define a
substitute _Static_assert macro for older GCC versions in a standard
internal include file. Then we could include that file and use
_Static_assert, regardless of GCC version.
Something like this, say:
#if __STDC_VERSION__ < 201112L
# define _Static_assert(e, s) extern int (*static_assert_checker (void)) \
[sizeof (struct { unsigned int error_if_negative: (e) ? 1 : -1; })]
#endif
This isn't a perfect emulation of _Static_assert (it can't be
used inside struct { ... }) but it'd be good enough for glibc's
internal purposes.
It's quite helpful to have a convenient way of making static assertions.
In Gnulib we define the equivalent of this:
#define verify(e) _Static_assert (e, "verify (" #e ")")
and we use 'verify' a lot. It'd be nice to have something similar
for glibc internals.
More information about the Libc-alpha
mailing list