Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #61675

Re: Implementing static_assert

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: Implementing static_assert
Date 2015-05-07 22:13 +1200
Message-ID <cr0s5fFl4ufU1@mid.individual.net> (permalink)
References <midqsq$81d$1@dont-email.me> <mif6mb$ffc$1@dont-email.me>

Show all headers | View raw


David Brown wrote:
> On 06/05/15 21:44, jacobnavia wrote:
>> I have added this useful construct to the lcc-win compiler.
>>
>> Description:
>>
>> #include <assert.h>
>> static_assert(boolean expression, message)
>>
>> The boolean expression will be evaluated at compile time.
>>
>> If the "boolean expression" is true at compile time, nothing happens,
>> the whole becomes a NOP (no operation).
>>
>> If the boolean expression fails, the character string "message" will be
>> printed in an error message. The compiler appends the failed expression
>> to the message. The compilation fails and no object code is generated.
>>
>> If the boolean expression can't be evaluated at compile time, the
>> compiler will generate a call to the _StaticAssert function, that will
>> print the error message at run time if the expression fails and exit the
>> program.
>
> I /strongly/ disapprove of the idea of turning the static assert into a
> run-time call if it can't be evaluated at compile-time.  I am not as
> experienced a "language lawyer" as Keith and some of the others here,
> but I believe the C11 standards insist that the boolean expression be
> compile-time constant - and I certainly think that /should/ be the case.
>   It is in the name - it is a /static/ assertion, and should have a
> /static/ effect.  So no matter what freedom the C11 standard may or may
> not give, give an error message if the expression is not constant.

I completely agree.  If the programmer doesn't realise the expression 
can't be evaluated at compile time, they could be lulled into a false 
sense of security.  A static_assert should fail a compile time, it is 
likely to be used to check the sanity of a build environment.  One 
example would be a continuous integration build that is built but never run.

-- 
Ian Collins

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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