I ran into a case where I had to swap out the value of a certain object. Due to my own sloppy copy and paste, I accidentally copied the type declaration as well. Here is a simplified example:
int main()
{
int i = 42;
cout << "i = " << i++ << endl;
// ... much later
if( isSwapRequired == true )
{
int i = 24;
cout << "i = " << i++ << endl;
}
cout << "i = " << i++ << endl;
}
To my dismay, the compiler did not catch this and further went on to let i = 24
live in its own little scope. Then later, it turns out that outside the scope, i
remains as 43
. I noticed that if both i
were in the same level, then the compiler would obligingly catch this mistake. Is there a reason for the compiler to treat the multiple declarations differently?
If it matters, I am using VS10.