title: Returning const reference to local variable from a function
body: <p>I have some questions on returning a reference to a local variable from a function:</p>
  
  <pre><code>class A {
  public:
      A(int xx)
      : x(xx)
      {
          printf("A::A()\n");
      }
  };
  
  const A&amp; getA1()
  {
      A a(5);
      return a;
  }
  
  A&amp; getA2()
  {
      A a(5);
      return a;
  }
  
  A getA3()
  {
      A a(5);
      return a;
  }
  
  int main()
  {
      const A&amp; newA1 = getA1(); //1
      A&amp; newA2 = getA2(); //2
      A&amp; newA3 = getA3(); //3
  }
  </code></pre>
  
  <p>My questions are =></p>
  
  <ol>
  <li><p>Is the implementation of <code>getA1()</code> correct? 
  I feel it is incorrect as it is returning the address of a local variable or temporary.</p></li>
  <li><p>Which of the statements in <code>main</code> (1,2,3) will lead to undefined behavior?</p></li>
  <li><p>In <code>const A&amp; newA1 = getA1();</code> does the standard guarantee that a temporary bound by a const reference will not be destroyed until the reference goes out of scope?</p></li>
  </ol>