Should this compile?

X

Xavier Decoret

for (int i=0;i<10;++i)
{
int i = -1;
cout<<i<<endl;
}

As far as I understand, the int declaration in the for statement makes
it declared for the scope of the for loop. So it would not be possible
to declare another int i.

On gcc-3.2.3, the above code does compile and displays 10 lines of -1.

Is it a standard compliant behaviour?

--
+-------------------------------------------------+
| Xavier Décoret - Post Doct |
| Graphics Lab (LCS) - MIT |
| mailto: (e-mail address removed) |
| home : http://www.graphics.lcs.mit.edu/~decoret|
+-------------------------------------------------+
 
J

John Harrison

Xavier Decoret said:
for (int i=0;i<10;++i)
{
int i = -1;
cout<<i<<endl;
}

As far as I understand, the int declaration in the for statement makes
it declared for the scope of the for loop. So it would not be possible
to declare another int i.

No that's wrong. A second declaration is possible, it hides the outer
declaration.
On gcc-3.2.3, the above code does compile and displays 10 lines of -1.

Is it a standard compliant behaviour?

Yes.

john
 
F

Fogus

If I remember correctly, the C++ grammar allows multiple declarations
of the same variable names within different scopes. However, I recall
that the Microsoft compilers did not allow this. I may be confused
however due to my long hiatis from C++, so hopefully someone will be
able to provide a definitive answer.

-m
 
R

Ron Natalie

Fogus said:
If I remember correctly, the C++ grammar allows multiple declarations
of the same variable names within different scopes. However, I recall
that the Microsoft compilers did not allow this. I may be confused
however due to my long hiatis from C++, so hopefully someone will be
able to provide a definitive answer.
You're confused. Even microsoft supports the program as posted. There
are two scopes, the for loop itself and the compound statement (the stuff
inside the braces). What Microsoft didn't get right (at least in the usual
configuration) was they didn't define a new scope for the for loop itself.
They put the variable declared there in the enclosing scope.
 
J

John Harrison

jeffc said:
But doesn't this make John Harrison's comment valid only for the (invalid)
Microsoft case?

Comeau C++ rejects the posted code. Looks like I was wrong (along with gcc
3.2.3).

john
 
R

Ron Natalie

Comeau C++ rejects the posted code. Looks like I was wrong (along with gcc
3.2.3).

Golly, that's right and danged inconsistant with the rest of the language. A variable
may not be decleared in the outermost block of the attached statement. An extra set
of braces would fix that.

The folowing is legal.

for (int i=0;i<10;++i)
{ {
int i = -1;
cout<<i<<endl;
} |
 
A

Andrew Ward

Visual C++ rejects the code with:
error C2374: 'i' : redefinition; multiple initialization
 
S

Samuel Barber

Xavier Decoret said:
for (int i=0;i<10;++i)
{
int i = -1;
cout<<i<<endl;
}

As far as I understand, the int declaration in the for statement makes
it declared for the scope of the for loop. So it would not be possible
to declare another int i.

On gcc-3.2.3, the above code does compile and displays 10 lines of -1.

Is it a standard compliant behaviour?

No. This is how C++ originally worked. At some point the scoping of
'for' declarations was redefined. Since this broke lots of C++ code,
many compilers implement the old behavior by default.

Consider:

#include <stdio.h>

int i;

int main(void)
{
for(int i=0; i<10; i++)
{
// int i; // legal in old C++; error under ISO C++
}

if(i==10)
printf("Old C++\n");
else
printf("ISO C++\n");

return 0;
}

When I invoke my compiler with no options, I get this:

test.cpp(11) : warning C4288: nonstandard extension used : 'i' : loop
control variable declared in the for-loop is used outside the for-loop
scope; it conflicts with the declaration in the outer scope
test.cpp(7) : definition of 'i' used
test.cpp(3) : definition of 'i' ignored

And the program prints "Old C++".

When invoked in "standard C++" mode, I get this:

test.cpp(11) : warning C4258: 'i' : definition from the for loop is
ignored; the definition from the enclosing scope is used
test.cpp(7) : definition of 'i' ignored
test.cpp(3) : definition of 'i' used

And the program prints "ISO C++".

Sam
 
J

Jason

With GCC you can specify -ansi, but that is for C only, as far as I know.
However, newer versions* may have a "strict" switch, have a look at the
command line options.

*My compiler was a bit old v3.0.2 something, compiled for ARM7 TDMI, that's
why I am using an older one. However, the -ansi switch for C works pretty
well.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top