This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
_Static_assert / verify for internal use in glibc
- From: Paul Eggert <eggert at cs dot ucla dot edu>
- To: "Joseph S. Myers" <joseph at codesourcery dot com>
- Cc: libc-alpha at sourceware dot org
- Date: Tue, 03 Dec 2013 12:56:46 -0800
- Subject: _Static_assert / verify for internal use in glibc
- Authentication-results: sourceware.org; auth=none
- References: <20131030174502 dot GA18107 at domone dot podge> <20131030183318 dot GA18706 at domone dot podge> <20131101133126 dot GA2546 at domone dot podge> <201311300346 dot 53198 dot vapier at gentoo dot org> <20131203111604 dot GA11582 at domone dot podge> <529E182F dot 8050809 at cs dot ucla dot edu> <Pine dot LNX dot 4 dot 64 dot 1312031747580 dot 7734 at digraph dot polyomino dot org dot uk>
[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.