Bug 21837 - C++/C99 standard violation in for loop
Summary: C++/C99 standard violation in for loop
Status: RESOLVED DUPLICATE of bug 2288
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.0
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-05-31 10:46 UTC by ahelm
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host: DJGPP
Target: MS-DOS
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description ahelm 2005-05-31 10:46:19 UTC
C++ standard quote:

3.3.2, paragraph 4:
Names declared in the for-init-statement, and in the condition of if, while,
for, and switch statements are local to the if, while, for, or switch statement
(including the controlled statement), and shall not be re-declared in a
subsequent condition of that statement nor in the outermost block (or, for the
if statement, any of the outermost blocks) of the controlled statement; see 6.4.

However g++/gcc happily compiles:

#include <stdio.h>

int main(void)
{
  for(int i=2;i<4;i++)
  {
    int j = i;
    int i;
    i = 555;
    printf("%d %d\n", i, j);
  }
  return 0;
}

and allows re-declaration of i. 
No warning/remark/error is given.
This should trigger an error.
Maybe it could be a gcc extension to the standard(s), but then it should
be clearly marked as such.

The C99 standard is a bit less clear about this but I believe this should also
apply to C99. 

Command lines used:
gxx -W -Wall -ansi -pedantic main.cpp
gcc -W -Wall -std=c99 -pedantic main.c
Comment 1 Gabriel Dos Reis 2005-05-31 11:16:40 UTC
Subject: Re:  New: C++/C99 standard violation in for loop

"ahelm at gmx dot net" <gcc-bugzilla@gcc.gnu.org> writes:

| C++ standard quote:
| 
| 3.3.2, paragraph 4:
| Names declared in the for-init-statement, and in the condition of if, while,
| for, and switch statements are local to the if, while, for, or switch statement
| (including the controlled statement), and shall not be re-declared in a
| subsequent condition of that statement nor in the outermost block (or, for the
| if statement, any of the outermost blocks) of the controlled statement; see 6.4.
| 
| However g++/gcc happily compiles:
| 
| #include <stdio.h>
| 
| int main(void)
| {
|   for(int i=2;i<4;i++)
|   {
|     int j = i;
|     int i;

There is duplicate of this bug in the case of C++, see PR2288.
Comment 2 joseph@codesourcery.com 2005-05-31 11:48:30 UTC
Subject: Re:  New: C++/C99 standard violation in for loop

On Tue, 31 May 2005, ahelm at gmx dot net wrote:

>   for(int i=2;i<4;i++)
>   {
>     int j = i;
>     int i;
>     i = 555;
>     printf("%d %d\n", i, j);
>   }

I don't see why you think there's any problem in C99 terms.  The for 
statement forms a block (6.8.5#5 first sentence); its body forms a block 
whose scope is a strict subset of that of the for statement (second 
sentence) and the compound statement is itself a block (whether the same 
block or a different one from that of the body as body doesn't matter in 
this case, but if the body is a labeled compound statement then you'd have 
distinct scopes).  As these are distinct blocks, they are distinct scopes; 
you can declare i in the block which contains the for statement, and in 
the declaration part of the for statement, and in the body of the for 
statement.

Comment 3 Andrew Pinski 2005-05-31 13:16:13 UTC
This is a dup of bug 2288.  C99 allows this as mentioned multiple times.

*** This bug has been marked as a duplicate of 2288 ***
Comment 4 ahelm 2005-05-31 17:41:43 UTC
Subject: Re:  C++/C99 standard violation in for loop

At 12:48 31/05/2005, joseph at codesourcery dot com wrote:
>
>------- Additional Comments From joseph at codesourcery dot com  2005-05-31 
>11:48 -------
>Subject: Re:  New: C++/C99 standard violation in for loop
>
>On Tue, 31 May 2005, ahelm at gmx dot net wrote:
>
>>   for(int i=2;i<4;i++)
>>   {
>>     int j = i;
>>     int i;
>>     i = 555;
>>     printf("%d %d\n", i, j);
>>   }
>
>I don't see why you think there's any problem in C99 terms.  The for 
>statement forms a block (6.8.5#5 first sentence); its body forms a block 
>whose scope is a strict subset of that of the for statement (second 
>sentence) and the compound statement is itself a block (whether the same 
>block or a different one from that of the body as body doesn't matter in 
>this case, but if the body is a labeled compound statement then you'd have 
>distinct scopes).  As these are distinct blocks, they are distinct scopes; 
>you can declare i in the block which contains the for statement, and in 
>the declaration part of the for statement, and in the body of the for 
>statement.

I knew I missed something...
The end of scope description in the "for" chapter, "end of the loop" sounded
rather nebulous to me, that's why I wasn't 100% sure about C99.
Thanks for pointing this out.

Regards,

Tony