Possible alternate definition of CTASSERT to allow its use in header files

From: Ryan Stone <rysto32_at_gmail.com>
Date: Tue, 30 Sep 2008 15:55:26 -0400
This was prompted by some recent check-ins removing CTASSERTs from
header files to prevent spurious errors from popping up.  For example,
this check-in:
http://lists.freebsd.org/pipermail/cvs-src/2008-September/095328.html

I've come up with an alternate definition of CTASSERT that can be used
in header files.  It works on gcc 3.4.6, 4.0.2 and 4.3.0(the only
compilers I have quick access to).

$ cat /tmp/tmp.c
// New definition
#define NEWASSERT(x) _NEWASSERT(x, __LINE__)
#define _NEWASSERT(x, line) __NEWASSERT(x, line)
#define __NEWASSERT(x, line) extern int __assert_ ## line [ x ? 1 : -1 ];

//existing BSD implementation
#define CTASSERT(x)             _CTASSERT(x, __LINE__)
#define _CTASSERT(x, y)         __CTASSERT(x, y)
#define __CTASSERT(x, y)        typedef char __assert ## y[(x) ? 1 : -1]

CTASSERT(1);                                      // line 11
CTASSERT(0);                                      // line 12
CTASSERT(1); CTASSERT(0);                // line 13


NEWASSERT(1);                                   // line 16
NEWASSERT(0) ;                                  // line 17
NEWASSERT(1); NEWASSERT(0);        // line 18
NEWASSERT(1); NEWASSERT(1);        // line 19


$ gcc -v -c /tmp/tmp.c -Wall -Werror
/tmp/tmp.c:12: error: size of array `__assert12' is negative
/tmp/tmp.c:13: error: size of array `__assert13' is negative
/tmp/tmp.c:13: error: redefinition of typedef '__assert13'
/tmp/tmp.c:13: error: previous declaration of '__assert13' was here
/tmp/tmp.c:17: error: size of array `__assert_17' is negative
/tmp/tmp.c:18: error: size of array `__assert_18' is negative
$

Note that the compiler doesn't complain about multiple definitions of
__assert18 and __assert19 like it does about the multiple definitions
of __assert13, which is the reason that CTASSERTs can't be used in
header files.

Thoughts?  Will this work on compilers other than gcc?
Received on Tue Sep 30 2008 - 18:23:33 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:39:35 UTC