While browsing some C++ code, I came across the following lines :
for (int i = 0; i < count; i++) {
if (&array[i].GetData() == el)
break;
}
if (i < count) {
// .. Do something
}
I am surpised to see that the loop-counter variable i is accessible outside the loop!
Just to ensure that the i outside the loop was same as the one inside the loop, I changed the loop variable name to i1.
for (int i1 = 0; i1 < count; i1++) {
if (&array[i1].GetData() == el)
break;
}
if (i < count) { // COMPILATION ERROR: Identifier i is undefined
// .. Do something
}
This resulted in a compilation error for the line if(i < count) :
identifier 'i' is undefined.
What is going on? This is too basic to be a compiler bug. If there was another i in a parent scope, there would have been no compilation error. Am I missing something? I am using Visual Studio 2015.
/permissiveand/or/Zaturned on?)/Zc:forScope-(" Force Conformance in For Loop Scope" in the IDE's C++ properties) option set? That causes the MC++ 2015 compiler to use legacy behavior that extends the scope offorloop variables.