[Bug c++/21837] New: C++/C99 standard violation in for loop

ahelm at gmx dot net gcc-bugzilla@gcc.gnu.org
Tue May 31 10:46:00 GMT 2005


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

-- 
           Summary: C++/C99 standard violation in for loop
           Product: gcc
           Version: 4.0.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ahelm at gmx dot net
                CC: gcc-bugs at gcc dot gnu dot org
  GCC host triplet: DJGPP
GCC target triplet: MS-DOS


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21837



More information about the Gcc-bugs mailing list