linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Robert P. J. Day" <rpjday@mindspring.com>
To: Glynn Clements <glynn@gclements.plus.com>
Cc: C programming list <linux-c-programming@vger.kernel.org>
Subject: Re: using compile-time "assertions"
Date: Mon, 6 Jun 2005 17:01:14 -0400 (EDT)	[thread overview]
Message-ID: <Pine.LNX.4.61.0506061655420.2617@localhost.localdomain> (raw)
In-Reply-To: <17059.41502.432000.929857@gargle.gargle.HOWL>

On Mon, 6 Jun 2005, Glynn Clements wrote:

>
> Robert P. J. Day wrote:
>
> >   another question (one of undoubtedly many) regarding the code i
> > inherited.  in this code, there is liberal use of the following macro:
> >
> > #define INVARIANT(e) \
> >   do { \
> >     struct whatever { char static_assertion[(e) ? 1 : -1]; }; \
> >     } while (0)
> >
> > which i recognize as a *compile-time* assertion/invariant.  is this a
> > common construct?  just curious.  or is there a more popular variant
> > of this sort of thing?  i recognize its value but, geez, there's
> > hundreds of these things in the code. :-P
>
> I've never seen that one before. It's more common to just use e.g.:
>
> 	#if !expression
> 	#error expression
> 	#endif

i'll admit that your alternative is the more obvious one for
compile-time checks, but i have to admit that INVARIANT macro is
growing on me since it's used literally *hundreds* of times in this
code base, and it fits conveniently on a single line and is eminently
readable.

admittedly, what you get if the invariant is violated is a pretty
generic error message:

  invar.c: In function `main':
  invar.c:15: error: size of array `static_assertion' is negative

but given that these are compile-time messages, one would think that
you'd be checking things that should *rarely* be false, and a file
name and line number are more than enough output.

i think i like this macro after all.

rday

      reply	other threads:[~2005-06-06 21:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-06-05 20:36 using compile-time "assertions" Robert P. J. Day
2005-06-06  1:08 ` Glynn Clements
2005-06-06 21:01   ` Robert P. J. Day [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.61.0506061655420.2617@localhost.localdomain \
    --to=rpjday@mindspring.com \
    --cc=glynn@gclements.plus.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).