Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: NeBuR on February 02, 2004, 09:06 AM

Title: [C/C++] New ISO 'for' loop scoping?
Post by: NeBuR on February 02, 2004, 09:06 AM
Hello, again  ;D.

I've found a problem with the for loop. Look at this code:


for (int counter = min1; counter < max1; counter++)
{
   ...   // Arbitrary code.
}

for (counter = min2; counter < max2; counter++)
{
   ...   // More arbitrary code.
}


Integer limit values of looping (min1, max1, min2 and max2) doesn't matter at all, while the problem is related to the 'counter' variable scope. I've verified that this code compiles without errors in M$ Visual C++ 6.0. But recently, trying to move an application to a UNIX enviroment, I've found the next error message while compiling it with GNU gcc 3.0.1:

Name lookup of `counter' changed for new ISO `for' scoping.
Using obsolete binding at `counter'.


Then I've tried the code with M$ Visual Studio .NET, and it accepts with no errors the last code fragment, as well as the next:


for (int counter = min1; counter < max1; counter++)
{
   ...   // Arbitrary code.
}

for (int counter = min2; counter < max2; counter++)
{
   ...   // More arbitrary code.
}


So my question is this: has the for loop been modified (by ISO) to consider variables declared in its header belonging to its internal scope?
Title: Re:[C/C++] New ISO 'for' loop scoping?
Post by: Zakath on February 02, 2004, 12:12 PM
As far as I know, this has always been the case. I frequently use the same variable name (usually "i") for all my for loops at the same scope, redeclaring it each time.

But yes, ISO C++ states that you should not rely on a variable declared in a for loop being available after the loop terminates.
Title: Re:[C/C++] New ISO 'for' loop scoping?
Post by: iago on February 02, 2004, 12:31 PM
I agree with Zak.  There's probably an option somewhere in VS to enable warnings for that, but it shouldn't even compile :/
Title: Re:[C/C++] New ISO 'for' loop scoping?
Post by: Raven on February 02, 2004, 01:30 PM
It's not a problem at all. A variable declared as part of the loop will remain local for only that loop. This can easily be avoided by declaring your counter variable outside the loop itself.
Title: Re:[C/C++] New ISO 'for' loop scoping?
Post by: Kp on February 02, 2004, 07:17 PM
Quote from: Zakath on February 02, 2004, 12:12 PM
As far as I know, this has always been the case.

No.  It used to be the way that VC accepts: treating a variable declared in the pre-loop component as declared immediately prior to the for, and thus valid for the remainder of the function.  The standard changed and VC was never updated to become compliant again.  gcc is compliant, but also permits you to do it the old way (and gives the warning Nebur noted).  Strictly speaking, VC is noncompliant with respect to the standard when it silently accepts code which fails to redeclare the variable.  Given that the noncompliance has propagated forward into VC7, I rather doubt they're going to fix it though.
Title: Re:[C/C++] New ISO 'for' loop scoping?
Post by: Yoni on February 03, 2004, 10:24 AM
Quote from: Kp on February 02, 2004, 07:17 PM
Given that the noncompliance has propagated forward into VC7, I rather doubt they're going to fix it though.
Actually, they fixed it in VC7, but the fix is disabled by default so that code that compiles under VC6 would work with no changes.

(http://yoni.valhallalegends.com/stuff/VC7ForLoopScope.png)

The fix is enabled either by selecting "Yes" for the highlighted option in the above screenshot (equivalent to "/Zc:forScope" for cl.exe), or by selecting "Yes" for "Disable Language Extensions" in the above screenshot (equivalent to "/Za" for cl.exe, the 'a' stands for ANSI).
Title: Re:[C/C++] New ISO 'for' loop scoping?
Post by: Kp on February 03, 2004, 02:07 PM
Quote from: Yoni on February 03, 2004, 10:24 AM
Actually, they fixed it in VC7, but the fix is disabled by default so that code that compiles under VC6 would work with no changes.

Interesting.  Is there any way to change the default, so that all subsequently created projects will default to conformance rather than nonconformance?
Title: Re:[C/C++] New ISO 'for' loop scoping?
Post by: Yoni on February 03, 2004, 03:02 PM
Not that I know of, should be worth looking into sometime though. (If you can do that, you can probably change other defaults to something less annoying than the default defaults - some of which I always change...)
Title: Re:[C/C++] New ISO 'for' loop scoping?
Post by: iago on February 03, 2004, 03:40 PM
Doesn't VC7 have support for Template projects?  If it does, it might be the easiest way to create a template, set the options the way you like it, and go from there.
Title: Re:[C/C++] New ISO 'for' loop scoping?
Post by: Skywing on February 05, 2004, 12:07 AM
Quote from: iago on February 03, 2004, 03:40 PM
Doesn't VC7 have support for Template projects?  If it does, it might be the easiest way to create a template, set the options the way you like it, and go from there.
Yes, it does.  Unfortunately, you seem to have to write the templates in JScript.

For loop scoping is hardly "new".