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


Groups > comp.lang.c > #61651

Re: Implementing static_assert

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Implementing static_assert
Date 2015-05-06 17:34 -0700
Organization None to speak of
Message-ID <87wq0ljkcz.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> <871titl7h9.fsf@kst-u.example.com> <mie6p1$p1l$1@dont-email.me>

Show all headers | View raw


Martin Shobe <martin.shobe@yahoo.com> writes:
> On 5/6/2015 4:30 PM, Keith Thompson wrote:
>> jacobnavia <jacob@jacob.remcomp.fr> writes:
[...]
>>> 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.)
>
> In the version I have (N1570), the constraint (6.7.10p2) reads, "The
> constant expression shall compare unequal to 0." Wouldn't that
> indicate a constraint violation for a non-constant expression?

Not directly, but a constraint violation is implied by the syntax.

The syntax is:

    static_assert-declaration:
        _Static_assert ( constant-expression , string-literal ) ;

A constant-expression is syntactically just a conditional-expression,
but the restrictions on constant-expressions are expressed as
constraints in N1570 6.6 p3-4.

I had missed that.  So using a non-constant expression in a
static_assert declaration *is* a constraint violation.

jacob, the required diagnostic can be a warning if you like, but it's
not optional if you want to follow the C11 definition, and I recommend
treating it as an error.

[...]

>> 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.
>
> The message is not optional. N3797 says
>
> Otherwise, the program is ill-formed, and the resulting diagnostic
> message (1.4) shall include the text of the string-literal, except
> that characters not in the basic source character set (2.3) are not
> required to appear in the diagnostic message.

So it does -- but N4296 gives the syntax as:

    static_assert-declaration:
        static_assert ( constant-expression ) ;
        static_assert ( constant-expression , string-literal ) ;

and says:

    Otherwise, the program is ill-formed, and the resulting diagnostic
    message (1.4) shall include the text of the string-literal, if one
    is supplied, except that characters not in the basic source
    character set (2.3) are not required to appear in the diagnostic
    message.

Since they made the string-literal optional, I'm a little surprised they
didn't require the message to include the text of the expression if the
string-literal is missing, as is already done for assert() (which takes
only an expression).

-- 
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


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