Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Keith Thompson <kst-u@mib.org> |
| Newsgroups | comp.lang.c |
| Subject | Re: Implementing static_assert |
| Date | Wed, 06 May 2015 14:30:10 -0700 |
| Organization | None to speak of |
| Lines | 62 |
| Message-ID | <871titl7h9.fsf@kst-u.example.com> (permalink) |
| References | <midqsq$81d$1@dont-email.me> <876185lach.fsf@kst-u.example.com> <midv1j$pin$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 8bit |
| Injection-Info | mx02.eternal-september.org; posting-host="9be37d943435275679235b07c7947eef"; logging-data="1880"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Od3GTr5WRhB5kIH0QHQYS" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
| Cancel-Lock | sha1:GR7nrXk+tk1ykqFQbt3uLNLdyTE= sha1:xjENofo+uc4v4NTlrhmngEItQMg= |
| Xref | csiph.com comp.lang.c:61645 |
Show key headers only | View raw
jacobnavia <jacob@jacob.remcomp.fr> writes:
> Le 06/05/2015 22:28, Keith Thompson a écrit :
>> jacobnavia <jacob@jacob.remcomp.fr> writes:
>>> I have added this useful construct to the lcc-win compiler.
>>>
>>> Description:
>>>
>>> #include <assert.h>
>>> static_assert(boolean expression, message)
>> [snip]
>>
>> See my reply in comp.compilers.lcc (I happened to see that first).
>>
>> Quick summary: This is standard in C11.
>>
>
> Again, thanks a lot for this. A summary review of my implementation
> and the standard looks like they are the same...
>
> I have just not understood what the static assertion in a structure
> declaration should do, but I have to read more carefully the stuff in
> the new C11 that I haven't really read that much.
I'm pretty sure having it inside a structure (or union) definition
doesn't affect anything. It's just permitted in that context for
convenience.
> Not specified is what to do when the expression is not a constant
> expression!
The standard says "The constant expression shall be an integer constant
expression." Since this is a "shall" outside a constraint, violating it
has undefined behavior. (I find that a little surprising; I would think
it would make more sense for a non-constant expression to be a
constraint violation.)
One popular C compiler treats a non-constant expression as an error.
That would be my preference as well. In C++, a non-constant expression
requires a diagnostic (if I'm reading the standard correctly).
> In my implementation I just call a run time procedure that will print
> the same error messages if the assert expression fails and return
> without doing anything if the assert expression is OK (different from
> zero).
>
> Since the behavior of a non constant expression is not specified I
> suppose that this is OK. In a strict mode I will issue a warning.
>
>
> What a surprise really. Yesterday I was looking at some C++ code and
> saw that construct and told me: "Interesting construct", and not that
> difficult to implement, so I did it today.
I think C11 may have borrowed static_assert from C++, with a change from
`static_assert` to `_Static_assert` to avoid breaking existing code.
One difference is that in C++ the message is optional.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Implementing static_assert jacobnavia <jacob@jacob.remcomp.fr> - 2015-05-06 21:44 +0200
Re: Implementing static_assert Keith Thompson <kst-u@mib.org> - 2015-05-06 13:28 -0700
Re: Implementing static_assert jacobnavia <jacob@jacob.remcomp.fr> - 2015-05-06 22:55 +0200
Re: Implementing static_assert Keith Thompson <kst-u@mib.org> - 2015-05-06 14:30 -0700
Re: Implementing static_assert Martin Shobe <martin.shobe@yahoo.com> - 2015-05-06 18:07 -0500
Re: Implementing static_assert Keith Thompson <kst-u@mib.org> - 2015-05-06 17:34 -0700
Re: Implementing static_assert David Brown <david.brown@hesbynett.no> - 2015-05-07 09:44 +0200
Re: Implementing static_assert Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-07 12:37 -0700
Re: Implementing static_assert David Brown <david.brown@hesbynett.no> - 2015-05-07 10:11 +0200
Re: Implementing static_assert Ian Collins <ian-news@hotmail.com> - 2015-05-07 22:13 +1200
Re: Implementing static_assert jacobnavia <jacob@jacob.remcomp.fr> - 2015-05-07 12:24 +0200
Re: Implementing static_assert Ian Collins <ian-news@hotmail.com> - 2015-05-07 22:33 +1200
Re: Implementing static_assert David Brown <david.brown@hesbynett.no> - 2015-05-07 14:30 +0200
Re: Implementing static_assert Philip Lantz <prl@canterey.us> - 2015-05-07 03:53 -0700
Re: Implementing static_assert David Brown <david.brown@hesbynett.no> - 2015-05-07 14:21 +0200
Re: Implementing static_assert Keith Thompson <kst-u@mib.org> - 2015-05-07 09:11 -0700
Re: Implementing static_assert James Kuyper <jameskuyper@verizon.net> - 2015-05-07 12:30 -0400
Re: Implementing static_assert James Kuyper <jameskuyper@verizon.net> - 2015-05-07 12:55 -0400
Re: Implementing static_assert Keith Thompson <kst-u@mib.org> - 2015-05-07 09:06 -0700
Re: Implementing static_assert jacobnavia <jacob@jacob.remcomp.fr> - 2015-05-07 19:43 +0200
Re: Implementing static_assert Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-07 16:10 -0700
csiph-web