31

I'm doing C++ in Visual Studio 2010 and found some odd behaviour. To make a long story short, I found that this won't compile:

for (int i = 0; i < 10; i++)
{
    int i = 11;
}

This seems correct, since the variable i is already declared in the for loop header.

Now, however if I insert another for-loop before the re-declaration of i, then suddenly the compiler, intellisense etc thiks the code is correct - giving no real warnings (Tried warnings level 3 and four (/W3 and /w4)). So, doing this will actually compile and run:

for (int i = 0; i < 10; i++)
{
    for(int j = 0; j < 5; j++)
    {
    }

    int i = 11;
}

Personally, I find it odd that insering another for-loop legitimates the otherwise same code scenario. Any kind spirit able to tell me what I'm overlooking here?

Thanks in advance!

EDIT: Wow, thanks everyone for all the replies and demos - You are awesome! :) This sample exposing a bug did cross my mind, I just assumed MS would have noticed such a thing by now and fixed it...at least in VS2013.

Tried changing the optimization settings as suggested, but it did not make any difference.

Thanks everybody!

First piece of code

Second piece of code

Credit for demos: @Mark Garcia

12
  • 1
    Is it the compiler or intellisense? Commented Nov 25, 2013 at 8:55
  • 2
    First piece of code, second piece of code. Commented Nov 25, 2013 at 8:58
  • @LuchianGrigore Compiler as well. VS2012 also rejects the first snippet and accepts the second. Commented Nov 25, 2013 at 9:00
  • What makes you think you're overlooking something? I 100% agree with your assessment. It is odd. Commented Nov 25, 2013 at 9:00
  • Print debugging the code: rextester.com/EJOYM5770 Commented Nov 25, 2013 at 9:02

2 Answers 2

19

According to the standard specification:

1 ... names declared in the for-init-statement are in the same declarative-region as those declared in the condition

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

and

4 Names declared in the for-init-statement, the for-range-declaration, and in the condition of if, while, for, and switch statements are local to the if, while, for, or switch statement (including the controlled statement), and shall not be redeclared in a subsequent condition of that statement nor in the outermost block (or, for the if statement, any of the outermost blocks) of the controlled statement [§3.3.3]

The behavior of MSVC++2010 is not standard and it's a bug.

Sign up to request clarification or add additional context in comments.

1 Comment

See also this answer to a somewhat related question (which agrees with your interpretation)
0

when you do something like:

 for (int i = 0; i < 10; i++)
    {
     //some code
    }

you are declaring variable i and are restricting it's scope to the for code block. So it'll only be visible inside the for loop. With that in mind, your first code snippet redefines variable i;

for (int i = 0; i < 10; i++)
 {
      int i;
 }

the compiler complains about a redefinition because you now have 2 variable with the same name, same data type and same scope.

As far as why the second piece of code compiles -compiler bug. It depends purely on the compiler implementation; if you change the optimization level it might not show up anymore.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.