declaration in for statement

L

Luca Forlizzi

The declaration part of a for statement is subject to the constraint
given in
Sec. 6.8.5 p3, which says "The declaration part of a for statement
shall only declare identifiers for objects having
storage class auto or register."

DR 277 clarifies that this constraint is violated if the declaration
declare an identifier that
is not an object, like a tag or an enumeration constant.

To me (as a non-native english speaker) it is not clear if the
constraint is violated
in the case where the declaration declares a new type without a tag
(if the new
type has a tag, then it clearly violates the constraint because the
tag is an
identifier not denoting an object), like for instance in:

for (struct { short a[2]; float b; } d = { {0, 0}, 0 }; d.b<10.5; d.b+
+) ...

I notice that gcc silently accept this construct, while producing
diagnostics in
cases where is present an identifier not denoting an object with auto
or register
storage class.
Is gcc right?
 
S

spygg

The declaration part of a for statement is subject to the constraint
given in
Sec. 6.8.5 p3, which says "The declaration part of a for statement
shall only declare identifiers for objects having
storage class auto or register."

DR 277 clarifies that this constraint is violated if the declaration
declare an identifier that
is not an object, like a tag or an enumeration constant.

To me (as a non-native english speaker) it is not clear if the
constraint is violated
in the case where the declaration declares a new type without a tag
(if the new
type has a tag, then it clearly violates the constraint because the
tag is an
identifier not denoting an object), like for instance in:

        for (struct { short a[2]; float b; } d = { {0, 0}, 0 }; d.b<10.5; d.b+
+) ...

I notice that gcc silently accept this construct, while producing
diagnostics in
cases where is present an identifier not denoting an object with auto
or register
storage class.
Is gcc right?

According to my eexperience " struct { short a[2]; float b; } d =
{ {0, 0}, 0 }; " is just a expression its vaule (as a whole)is 1 or
0
PS:I am new learner ,just give my opinion and wait for C cattle man^_^
 
B

Ben Bacarisse

I was going to leave this alone, but since I am commenting on a comment...
To me (as a non-native english speaker) it is not clear if the
constraint is violated
in the case where the declaration declares a new type without a tag
(if the new
type has a tag, then it clearly violates the constraint because the
tag is an
identifier not denoting an object), like for instance in:

        for (struct { short a[2]; float b; } d = { {0, 0}, 0 }; d.b<10.5; d.b+
+) ...

I'd say the common interpretation is that any declaration of anything
but an identifier for an object (of the right storage class) is a
constraint violation. The key, then, is what is declared by that 'for'
statement?

There is some ambiguity in the meaning of the word "declare". It would
be nice if identifiers were always declared and objects, types and
function were defined. This is a distinction that is common when
talking about programming languages in an abstract way, but it seems
that the C standard does not want to use this distinction. Some
sections talk about declaring is structure type with or without a tag.

The question then hangs on whether 'struct { int i; } d;' declares
anything other than the identifier d. From the language used in, say,
6.2.7 p1 I'd have to say that this declares a structure type as well and
so it does not "only declare identifiers for objects...".

Part of me would like to "correct" the standard and say that 'struct {
int i; } d;' *declares* the identifier 'd' and also *defines* a structure
type at the same time. That's how I'd talk about it, but the meaning of
the word 'declare' is important for 6.8.5 p3 and it seems it includes
what I would casually call a structure type definition.

I put "correct" in quotes because there is nothing wrong with the usage
in the standard. In particular it follows from the fact the a
"declaration" is syntax name that covers everything from function
definitions to external object declarations.
I notice that gcc silently accept this construct, while producing
diagnostics in
cases where is present an identifier not denoting an object with auto
or register
storage class.
Is gcc right?

According to my eexperience " struct { short a[2]; float b; } d =
{ {0, 0}, 0 }; " is just a expression its vaule (as a whole)is 1 or
0

It's a declaration, not an expression. As for the 0 or 1 I don't see
how you come to that conclusion. Are you confusing = with ==?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top