title: C++ Redeclaring a variable in a sub-scope does not cause a compile error when it should?
body: <p>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:</p>
  
  <pre><code>int main()
  {
      int i = 42;
      cout &lt;&lt; "i = " &lt;&lt; i++ &lt;&lt; endl;
  
      // ... much later
  
      if( isSwapRequired == true )
      {
          int i = 24;
          cout &lt;&lt; "i = " &lt;&lt; i++ &lt;&lt; endl;
      }
      cout &lt;&lt; "i = " &lt;&lt; i++ &lt;&lt; endl;
  }
  </code></pre>
  
  <p>To my dismay, the compiler did not catch this and further went on to let <code>i = 24</code> live in its own little scope. Then later, it turns out that outside the scope, <code>i</code> remains as <code>43</code>. I noticed that if both <code>i</code> 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?</p>
  
  <p>If it matters, I am using VS10.</p>