* using compile-time "assertions"
@ 2005-06-05 20:36 Robert P. J. Day
2005-06-06 1:08 ` Glynn Clements
0 siblings, 1 reply; 3+ messages in thread
From: Robert P. J. Day @ 2005-06-05 20:36 UTC (permalink / raw)
To: C programming list
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
rday
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: using compile-time "assertions"
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
0 siblings, 1 reply; 3+ messages in thread
From: Glynn Clements @ 2005-06-06 1:08 UTC (permalink / raw)
To: Robert P. J. Day; +Cc: C programming list
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
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: using compile-time "assertions"
2005-06-06 1:08 ` Glynn Clements
@ 2005-06-06 21:01 ` Robert P. J. Day
0 siblings, 0 replies; 3+ messages in thread
From: Robert P. J. Day @ 2005-06-06 21:01 UTC (permalink / raw)
To: Glynn Clements; +Cc: C programming list
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-06-06 21:01 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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).