<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2813: std::function should not return dangling references</title>
<meta property="og:title" content="Issue 2813: std::function should not return dangling references">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2813.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#Resolved">Resolved</a> status.</em></p>
<h3 id="2813"><a href="lwg-defects.html#2813">2813</a>. <code>std::function</code> should not return dangling references</h3>
<p><b>Section:</b> 22.10.17.3.2 <a href="https://wg21.link/func.wrap.func.con">[func.wrap.func.con]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Brian Bi <b>Opened:</b> 2016-11-03 <b>Last modified:</b> 2022-11-25</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#func.wrap.func.con">issues</a> in [func.wrap.func.con].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
If a <code>std::function</code> has a reference as a return type, and that reference binds to a prvalue 
returned by the callable that it wraps, then the reference is always dangling. Because any use of such 
a reference results in undefined behaviour, the <code>std::function</code> should not be allowed to be 
initialized with such a callable. Instead, the program should be ill-formed.
<p/>
A minimal example of well-formed code under the current standard that exhibits this issue:
</p>
<blockquote><pre>
#include &lt;functional&gt;

int main() 
{
  std::function&lt;const int&amp;()&gt; F([]{ return 42; });
  int x = F(); // oops!
}
</pre></blockquote>

<p><i>[2016-11-22, David Krauss comments and suggests wording]</i></p>

<p>
Indirect bindings may also introduce temporaries inside <code>std::function</code>, e.g.:
</p>
<blockquote><pre>
void f(std::function&lt;long const&amp;()&gt;); // Retains an observer to a long.

void g() {
  int v;
  f([&amp;]()-&gt;int&amp; { return v; } ); // int lvalue binds to long const&amp; through a temporary.
}
</pre></blockquote>
<p>
A fix has been implemented. Conversions that may be conversion operators are allowed, though, because those can 
produce legitimate glvalues. Before adopting this, it need to be considered considered whether there should be 
SFINAE or a hard error.
</p>

<p><i>[Issues Telecon 16-Dec-2016]</i></p>

<p>Priority 2</p>

<p><i>[2016-07, Toronto Saturday afternoon issues processing]</i></p>

<p>Billy to work with Brian to rework PR. Status to Open</p>

<p><i>[2018-08-23 Batavia Issues processing]</i></p>

<p>Really needs a language change to fix this. Status to EWG.</p>

<p><i>[2022-08-24 Resolved by <a href="https://wg21.link/P2255R2" title=" A type trait to detect reference binding to temporary">P2255R2</a>. Status changed: EWG &rarr; Resolved.]</i></p>


<p><i>[2022-11-25; see <a href="https://github.com/cplusplus/papers/issues/1370">EWG 1370</a>]</i></p>




<p id="res-2813"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4618">N4618</a>.
</p>
<ol>
<li><p>Add a second paragraph to the remarks section of 22.10.17.3.2 <a href="https://wg21.link/func.wrap.func.con">[func.wrap.func.con]</a>:</p>
<blockquote>
<pre>
template&lt;class F&gt; function(F f);
</pre>
<blockquote>
<p>
-7- <i>Requires:</i> <code>F</code> shall be <code>CopyConstructible</code>.
<p/>
-8- <i>Remarks:</i> This constructor template shall not participate in overload resolution unless 
</p>
<ul>
<li><p><code>F</code> is Lvalue-Callable (22.10.17.3 <a href="https://wg21.link/func.wrap.func">[func.wrap.func]</a>) for argument types <code>ArgTypes...</code> and 
return type <code>R</code><ins>, and</ins></p></li>
<li><p><ins>If <code>R</code> is type "reference to <code>T</code>" and <code><i>INVOKE</i>(ArgTypes...)</code> has value category 
<code><i>V</i></code> and type <code>U</code>:</ins></p>
<ul>
<li><p><ins><code><i>V</i></code> is a prvalue, <code>U</code> is a class type, and <code>T</code> is not reference-related 
(9.5.4 <a href="https://wg21.link/dcl.init.ref">[dcl.init.ref]</a>) to <code>U</code>, and</ins></p></li>
<li><p><ins><code><i>V</i></code> is an lvalue or xvalue, and either <code>U</code> is a class type or <code>T</code> is 
reference-related to <code>U</code></ins>.</p></li>
</ul>
</li>
</ul>
<p>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
