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.)
VC6 is rather old, and not always … rigid … in its application of the standard 🙂 It actually leaked scope in certain circumstances like:
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{...}:Section
6.5.3covers the scope of variables “created” by aforstatement: