title: Why do lambda functions drop deduced return type reference by default?
body: <p>In C++14, why do lambda functions with a deduced return type drop references from the return type by default? IIUC, since C++14 lambda functions with a deduced return type (without an explicit trailing return type) have a return type of <code>auto</code>, which drops references (among other things).</p>
  
  <p>Why was this decision made? It seems to me like a gotcha to remove a reference when that's what your return statement returns.</p>
  
  <p>This behavior caused the following nasty bug for me:</p>
  
  <pre><code>class Int {
  public:
     Int(int i) : m_int{i} {}
     int m_int;
  };
  
  class C {
  public:
      C(Int obj) : m_obj{obj} {}
      const auto&amp; getObj() { return m_obj; }
      Int m_obj;
  };
  
  class D {
  public:
      D(std::function&lt;const Int&amp;()&gt; f) : m_f{f} {}
      std::function&lt;const Int&amp;()&gt; m_f;
  };
  
  Int myint{5};
  C c{myint};
  D d{ [&amp;c](){ return c.getObj(); } } // The deduced return type of the lambda is Int (with no reference)
  const Int&amp; myref = d.m_f(); // Instead of referencing myint, myref is a dangling reference; d.m_f() returned a copy of myint, which is subsequently destroyed.
  </code></pre>
  
  <p>Specifying the desired return type when initializing <code>d</code> resolves the issue:</p>
  
  <pre><code>D d{ [&amp;c]() -&gt; const Int&amp; { return c.getObj(); } }
  </code></pre>
  
  <p>Interestingly, even if the <code>auto</code> return type deduction makes sense, isn't it a bug that <code>std::function&lt;const Int&amp;&gt;</code> gets happily initialized with a function that returns a non-reference? I see this also by writing explicitly:</p>
  
  <pre><code>D d{ [&amp;c]() -&gt; Int { return c.getObj(); } }
  </code></pre>
  
  <p>which compiles without a problem. (on <code>Xcode 8</code>, <code>clang 8.0.0</code>)</p>