title: In C++, when can two variables of the same name be visible in the same scope?
body: <p>This code illustrates something that I think should be treated as bad practice, and elicit warnings from a compiler about redefining or masking a variable:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int *a;
  
  int* f()
  {
    int *a = new int;
    return a;
  }
  
  int main()
  {
    std::cout &lt;&lt; a &lt;&lt; std::endl &lt;&lt; f() &lt;&lt; std::endl;
    return 0;
  }
  </code></pre>
  
  <p>Its output (compiled with g++):</p>
  
  <pre><code>0
  0x602010
  </code></pre>
  
  <p>I've looked at a couple references (Stroustrup and The Complete C++ Reference) and can't find anything about when and why this is allowed. I know that it's not within a single local scope, though.</p>
  
  <p>When and why is this allowed? Is there a good use for this construct? How can I get g++ to warn me about it? Do other compilers squawk about it?</p>