Re: Reason for ISO ; warning



Wolfgang Riedel wrote:
Simon Biber wrote:
Wolfgang Riedel wrote:
so this should be valid (and compiles with -Wall -pedantic -std=c99):

#include <string.h>

size_t blah = strlen("abc");

/*...*/
It's not valid, and gcc produces the diagnostic message "initializer
element is not constant" when compiled with exactly the options you give.

--
Simon.

You snipped the rationale, I quoted. I think it's correct (evaluates to arithmetic constant exrpssion).

No. The relevant definition is

"An arithmetic constant expression shall have arithmetic type and shall only have operands that are integer constants, floating constants, enumeration constants, character constants, and sizeof expressions. Cast operators in an arithmetic constant expression shall only convert arithmetic types to arithmetic types, except as part of an operand to a sizeof operator whose result is an integer constant."

'strlen("abc")' doesn't qualify. gcc is free to evaluate 'strlen("abc")' as a constant expression, but this does not make it an arithmetic constant expression. Of course, the initializer to "blah" need only be a constant expression. It doesn't have to be an arithmetic constant expression specifically.

Btw. my gcc doesn't warn (maybe not the latest & greatest):

<snip>
Not a bug. Later versions of gcc do warn because standards-conforming compilers are not required to accept the program, but gcc does not violate the standard by not issuing a diagnostic -- implementations are expressly allowed to accept other forms of expressions as constant than the ones described by the standard.


If you expect 'strlen("abc")' to be a constant expression, you're relying on an extension. It's not portable code.

Note that gcc does *not* allow expressions that cannot be evaluated at compile time as initializers, and rightly so. This will not compile:

    size_t foo();
    size_t blah = foo();

S.
.



Relevant Pages

  • Re: Complex litterals
    ... Such a constant expression shall be, ... arithmetic constant expression shall only convert arithmetic ... I had not thought about the fact that compound literals are listed as one of the types of postfix operators, when I made my previous comment. ... However, I now think that 6.6p8 should be interpreted as allowing a compound literal as an an arithmetic constant expression, so long as it's type and initialization expressions meet the other requirements of that section. ...
    (comp.std.c)
  • Re: C2124 is most disrespectful of IEEE floating point arithmetic
    ... "If during the evaluation of an expression, ... a constant expression, in which case the program is illformed. ... enumeration types to arithmetic or enumeration types, ...
    (microsoft.public.vc.language)
  • Re: C2124 is most disrespectful of IEEE floating point arithmetic
    ... "An arithmetic constant expression shall satisfy the requirements for ... Thus 1.0f/0.0f is an "arithmetic constant expression". ... Undefined behaviour is just that, ... deduce about code correctness by looking how compilers behave. ...
    (microsoft.public.vc.language)
  • Re: (int)"text" as initializer
    ... And it is verly likely for the committee to reject a DR to request ... defined form of the arithmetic constant expression. ... Jun, Woong ...
    (comp.std.c)
  • Re: Initializer element not constant
    ... pointer to unsigned int on a 64-bit implementation. ... An integer constant expression96) shall have integer type and shall ... Such a constant expression shall be, or evaluate to, one of the ... shall only convert arithmetic types to arithmetic types, ...
    (comp.lang.c)