473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Should this compile?

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: de*****@graphics.lcs.mit.edu |
| home : http://www.graphics.lcs.mit.edu/~decoret|
+-------------------------------------------------+

Jul 19 '05 #1
8 2672

"Xavier Decoret" <de*****@graphics.lcs.mit.edu> wrote in message
news:bi***********@grapevine.lcs.mit.edu...
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
Jul 19 '05 #2
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 <de*****@graphics.lcs.mit.edu> wrote in message news:<bi***********@grapevine.lcs.mit.edu>...
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?

Jul 19 '05 #3

"Fogus" <no****@lacrymology.com> wrote in message news:d1*************************@posting.google.co 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.
Jul 19 '05 #4

"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Fogus" <no****@lacrymology.com> wrote in message

news:d1*************************@posting.google.co 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
Jul 19 '05 #5


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

Jul 19 '05 #6
Visual C++ rejects the code with:
error C2374: 'i' : redefinition; multiple initialization

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...

"Fogus" <no****@lacrymology.com> wrote in message news:d1*************************@posting.google.co 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.

Jul 19 '05 #7
Xavier Decoret <de*****@graphics.lcs.mit.edu> wrote in message news:<bi***********@grapevine.lcs.mit.edu>...
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
Jul 19 '05 #8
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.
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: CY FOK | last post by:
Hi I am planning to open a software company to develop client-server apps and web applications for my client. Now, i am in a difficult situation to determine what is the best platform i should use...
19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
7
by: Aguilar, James | last post by:
Hello all, To begin, yes, this -is- a homework assignment. However, it is for my Algorithms class, and my instructor has given us explicit permission to use "expert" groups like newsgroups, so...
7
by: BigMan | last post by:
Should the following piece of code compile or not according the C++ standard? Why? class t { public: t( ) { } t( t& a ) { } template< typename ttt >
6
by: Andre Eisenbach | last post by:
Should this code compile or not? class A { void f(); }; class B { friend void A::f();
3
by: Yang Zhang | last post by:
Here is a program: ///////////////////////////////////////////////// #include <iostream> using namespace std ; class A { int a ; A(const A& aA) { a=aA.a ; cout<<"copy constructor...
3
by: Shapper | last post by:
Hello, I am starting 2 new projects to deliver in January 2006. I want to create them in Asp.Net 2.0 using Visual Studio 2005. All my clients web sites are Visual Studio 2003 projects in...
6
by: Abubakar | last post by:
Hi, I'm working on a project in unmanaged c++. I was writing all (most of) my code in header files, that is, no seperation of code in header and cpp files, as usually is done in c++. I feel pretty...
6
by: Howard Gardner | last post by:
/* As it sits, this program won't compile for me. It will compile using either of the versions of tpt that I've commented out. Which versions SHOULD compile? I think that the code is...
4
by: John | last post by:
6.3.2.3 seems to say "yes", but 6.5.16.1 seems to say "no". Reason I'm asking is that the Ogg Vorbis source uses this pattern, and although it compiles fine on MSC and gcc 4, it does not compile...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.