Variable Scope in C++
Asked Answered
B

5

9

If I had the following code:

for(int myvar = 0; myvar < 10; myvar++);
if(1)
{
    int var2 = 16;
}

Then, afterwards I wrote the following:

myvar = 0;
var2 = 0;

Would that be legal? My VC++6 compiles it correctly, but I think this should be illegal. (It gives a compiler error in one of my other compilers.)

Bendix answered 2/11, 2010 at 6:5 Comment(2)
"My VC++6 compiles it correctly," no it doesn't.Westernize
@all answers, I have VC++6 only on one computer; on my other, I have VC++2008EE. I'm forced to use VC++6. :)Bendix
R
11

VC6 is rather old, and not always ... rigid ... in its application of the standard :-) It actually leaked scope in certain circumstances like:

for (int i = 0; i < 10; i++) { }
// You can still use 'i' here.

This led to some funky macro magic to get around this problem. If you're using a ISO-conformant compiler, both those things you try to do are illegal.

From ISO C++11 3.3.3/1, dealing with the introduction of block scope with {...}:

A name declared in a block is local to that block; it has block scope. Its potential scope begins at its point of declaration and ends at the end of its block.

Section 6.5.3 covers the scope of variables "created" by a for statement:

If the for-init-statement is a declaration, the scope of the name(s) declared extends to the end of the for-statement.

Reforest answered 2/11, 2010 at 6:10 Comment(0)
S
17

No, it would not be (§3.3.2 Local scope):

  1. A name declared in a block (6.3) is local to that block. Its potential scope begins at its point of declaration (3.3.1) and ends at the end of its declarative region.

I recommend you use compilers released in the last decade.

Slier answered 2/11, 2010 at 6:6 Comment(0)
R
11

VC6 is rather old, and not always ... rigid ... in its application of the standard :-) It actually leaked scope in certain circumstances like:

for (int i = 0; i < 10; i++) { }
// You can still use 'i' here.

This led to some funky macro magic to get around this problem. If you're using a ISO-conformant compiler, both those things you try to do are illegal.

From ISO C++11 3.3.3/1, dealing with the introduction of block scope with {...}:

A name declared in a block is local to that block; it has block scope. Its potential scope begins at its point of declaration and ends at the end of its block.

Section 6.5.3 covers the scope of variables "created" by a for statement:

If the for-init-statement is a declaration, the scope of the name(s) declared extends to the end of the for-statement.

Reforest answered 2/11, 2010 at 6:10 Comment(0)
C
4

That should be illegal but VC6 was very bad at that.

In Visual Studio 2005, a new project-level setting was introduced named "Force conformance in For-loop Scope". This addressed the problem and provided backward compatibility as well. Which means that older code bases could compile in newer versions of visual studio through disabling this setting.

However, one thing that MS did right in VS2005 to turn this on by default hence, coming a little bit closer to the standards.

Caravette answered 2/11, 2010 at 7:59 Comment(0)
R
1

Would that be legal? My VC++6 compiles it correctly, but I think this should be illegal.

No it shouldn't be legal. Dump VC++6. Use a new and better compiler.

Refraction answered 2/11, 2010 at 6:10 Comment(0)
H
1

VC took about a decade to implement proper scope for variables declared in loops and conditional statements. Generally you can't rely on VC6' judgment regarding C++.

Horal answered 2/11, 2010 at 6:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.