title: When is temporary lifetime extension useful in modern C++?
body: <p>In C++ you can bind return value of a function (which return value, not reference) to const reference and code will still be valid because lifetime of this temporary will be prolonged till the end of scope. For example</p>
  
  <pre><code>std::string get_string() {
      return "abc";
  }
  
  
  void f() {
      const std::string&amp; str = get_string();
      std::cout &lt;&lt; str; // valid, str is not dangling reference.
  }
  </code></pre>
  
  <p>My question is, when is it useful, e.g. when is code like</p>
  
  <pre><code>A get_a();
  const A&amp; a = get_a();
  </code></pre>
  
  <p>better than the code</p>
  
  <pre><code>A get_a();
  A a = get_a();
  </code></pre>
  
  <p>and in what way (e.g. faster, smaller binary size, etc)? What should be the implementations of <code>A</code>, <code>get_a</code> and code after calling <code>get_a</code>?</p>
  
  <p>I've tested several cases by hand and in each case it seems to have the same number of copies and moves.</p>
  
  <p>Let's restrict this question to current C++ standard, modern versions of compilers and builds with optimisation enabled (O2, O3 or equivalents for other compilers)</p>