Valhalla Legends Forums Archive | C/C++ Programming | [C/C++] New ISO 'for' loop scoping?

AuthorMessageTime
NeBuR
Hello, again ;D.

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

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

for (counter = min2; counter < max2; counter++)
{
   ...   // More arbitrary code.
}
[/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:

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

for (int counter = min2; counter < max2; counter++)
{
   ...   // More arbitrary code.
}
[/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?
February 2, 2004, 3:06 PM
Zakath
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.
February 2, 2004, 6:12 PM
iago
I agree with Zak. There's probably an option somewhere in VS to enable warnings for that, but it shouldn't even compile :/
February 2, 2004, 6:31 PM
Raven
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.
February 2, 2004, 7:30 PM
Kp
[quote author=Zakath link=board=30;threadid=5048;start=0#msg42298 date=1075745575]
As far as I know, this has always been the case.[/quote]

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.
February 3, 2004, 1:17 AM
Yoni
[quote author=Kp link=board=30;threadid=5048;start=0#msg42379 date=1075771020]
Given that the noncompliance has propagated forward into VC7, I rather doubt they're going to fix it though.
[/quote]
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.

[img]http://yoni.valhallalegends.com/stuff/VC7ForLoopScope.png[/img]

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).
February 3, 2004, 4:24 PM
Kp
[quote author=Yoni link=board=30;threadid=5048;start=0#msg42450 date=1075825483]
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.[/quote]

Interesting. Is there any way to change the default, so that all subsequently created projects will default to conformance rather than nonconformance?
February 3, 2004, 8:07 PM
Yoni
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...)
February 3, 2004, 9:02 PM
iago
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.
February 3, 2004, 9:40 PM
Skywing
[quote author=iago link=board=30;threadid=5048;start=0#msg42475 date=1075844436]
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.
[/quote]
Yes, it does. Unfortunately, you seem to have to write the templates in JScript.

For loop scoping is hardly "new".
February 5, 2004, 6:07 AM

Search