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!
Credit for demos: @Mark Garcia