Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Should this compile?

Reply
Thread Tools

Should this compile?

 
 
Xavier Decoret
Guest
Posts: n/a
 
      08-26-2003
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: http://www.velocityreviews.com/forums/(E-Mail Removed) |
| home : http://www.graphics.lcs.mit.edu/~decoret|
+-------------------------------------------------+

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-26-2003

"Xavier Decoret" <(E-Mail Removed)> wrote in message
news:bifsjb$19e6$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Fogus
Guest
Posts: n/a
 
      08-26-2003
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



Xavier Decoret <(E-Mail Removed)> wrote in message news:<bifsjb$19e6$(E-Mail Removed)>...
> 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?

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      08-26-2003

"Fogus" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) m...
> 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.


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-26-2003

"jeffc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Ron Natalie" <(E-Mail Removed)> wrote in message
> news:3f4b9a5b$0$23258$(E-Mail Removed) m...
> >
> > "Fogus" <(E-Mail Removed)> wrote in message

> news:(E-Mail Removed) m...
> > > 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.

>
> 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


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      08-26-2003


>
> 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;
} |



 
Reply With Quote
 
Andrew Ward
Guest
Posts: n/a
 
      08-27-2003
Visual C++ rejects the code with:
error C2374: 'i' : redefinition; multiple initialization



"Ron Natalie" <(E-Mail Removed)> wrote in message
news:3f4b9a5b$0$23258$(E-Mail Removed) m...
>
> "Fogus" <(E-Mail Removed)> wrote in message

news:(E-Mail Removed) m...
> > 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.
>
>



 
Reply With Quote
 
Samuel Barber
Guest
Posts: n/a
 
      08-27-2003
Xavier Decoret <(E-Mail Removed)> wrote in message news:<bifsjb$19e6$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Jason
Guest
Posts: n/a
 
      08-27-2003
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
gems should *not be case sensitive.. or should they? botp Ruby 6 10-04-2010 11:42 PM
What the FAQs should and should not contain Josef 'Jupp' SCHUGT Ruby 0 08-19-2005 01:46 PM
Should I Bridge? =?Utf-8?B?Zmx1ZmZ5IHRoZSB3b25kZXIga2l0dGVu?= Wireless Networking 1 07-21-2005 01:25 AM
taking 70-290 should i be scared? What should i expect??? Raymond Munyan MCSE 31 12-01-2004 02:34 PM
How should control images should be handled? ~~~ .NET Ed ~~~ ASP .Net Building Controls 1 11-03-2004 12:30 PM



Advertisments