title: Temporary lifetime extension
body: <p>Section 12.2.5 of the standard says:</p>
  <blockquote>
  <p>A temporary bound to a reference parameter in a function call (5.2.2)
  persists until the completion of the full expression containing the
  call. A temporary bound to the returned value in a function return
  statement (6.6.3) persists until the function exits. In all these
  cases, the temporaries created during the evaluation of the expression
  initializing the reference, except the temporary to which the
  reference is bound, are destroyed at the end of the full-expression in
  which they are created and in the reverse order of the completion of
  their construction.</p>
  </blockquote>
  <p>I'm trying to understand the following code:</p>
  <pre><code>#include &lt;iostream&gt;
  
  const int&amp; foo(const int&amp; fooRef)
  {
      return fooRef;
  }                                        // #0
  
  int main (void)
  {
      const int&amp; numberRef = foo(5);     // #1
      std::cout &lt;&lt; numberRef;            // #2
      return 0;
  }
  </code></pre>
  <p>On line <code>#1</code> a temporary object is created and bound to <code>fooRef</code>. <code>fooRef</code> is destroyed on line <code>#0</code>. I thought the temporary should be destroyed here since lifetime-extension is not transitive.</p>
  <p>Questions:</p>
  <ol>
  <li><p>What does <code>until the function exits</code> mean? Does it mean <code>untill it finished executing</code>?</p>
  </li>
  <li><p>Why do I get a <code>5</code> output. Does a temporary object still exist on line <code>#2</code>?</p>
  </li>
  <li><p>How can I interpret the standard quote to figure out how this example works?</p>
  </li>
  </ol>
  <p>Step-by-step atomic walk-through with references to the standard would be greatly appreciated. Thank you!</p>
  <p>P. S. An accepted answer <a href="https://stackoverflow.com/questions/17236007/corrupted-const-reference-to-a-temporary-object-life-time">here</a> also told the the code is <code>broken</code> and I do not get, why I get such output of program.</p>