<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3238: Insufficiently-defined behavior of std::function deduction guides</title>
<meta property="og:title" content="Issue 3238: Insufficiently-defined behavior of std::function deduction guides">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3238.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#C++20">C++20</a> status.</em></p>
<h3 id="3238"><a href="lwg-defects.html#3238">3238</a>. Insufficiently-defined behavior of <code>std::function</code> deduction guides</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#C++20">C++20</a>
 <b>Submitter:</b> Louis Dionne <b>Opened:</b> 2019-07-17 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>Not Prioritized
</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#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The following code is currently undefined behavior:
</p>
<blockquote><pre>
#include &lt;functional&gt;

struct R { };
struct f0 { R operator()() &amp;&amp; { return {}; } };

int main() { std::function f = f0{}; }
</pre></blockquote>
<p>
The reason is that 22.10.17.3.2 <a href="https://wg21.link/func.wrap.func.con">[func.wrap.func.con]</a>/12 says:
</p>
<blockquote><p>
This deduction guide participates in overload resolution only if <code>&amp;F::operator()</code> is well-formed 
when treated as an unevaluated operand. In that case, if <code>decltype(&amp;F::operator())</code> is of the 
form <code>R(G::*)(A...) <i>cv</i> &amp;<sub><i>opt</i></sub> noexcept<sub><i>opt</i></sub></code> for a 
class type <code>G</code>, then the deduced type is <code>function&lt;R(A...)&gt;</code>.
</p></blockquote>
<p>
However, it does not define the behavior when <code>&amp;F::operator()</code> is well-formed but <em>not</em> 
of the required form (in the above example it's of the form <code>R(G::*)(A...) &amp;&amp;</code>, which is 
rvalue-reference qualified instead of optionally-lvalue-reference qualified). libc++'s implementation 
of the deduction guide SFINAE's out when either <code>&amp;F::operator()</code> is not well-formed, or it 
is not of the required form. It seems like mandating that behavior in the Standard is the way to go.
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4820">N4820</a>.</p>

<ol>
<li><p>Modify 22.10.17.3.2 <a href="https://wg21.link/func.wrap.func.con">[func.wrap.func.con]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class F&gt; function(F) -&gt; function&lt;<i>see below</i>&gt;;
</pre><blockquote>
<p>
-12- <i>Remarks:</i> This deduction guide participates in overload resolution only if 
<code>&amp;F::operator()</code> is well-formed when treated as an unevaluated operand<ins>, and its type is 
of the form <code>R(G::*)(A...) <i>cv</i> &amp;<sub><i>opt</i></sub> noexcept<sub><i>opt</i></sub></code> 
for a class type <code>G</code> and a sequence of types <code>A...</code></ins>. 
In that case, <del>if <code>decltype(&amp;F::operator())</code> is of the form <code>R(G::*)(A...) <i>cv</i> 
&amp;<sub><i>opt</i></sub> noexcept<sub><i>opt</i></sub></code> for a class type <code>G</code>, 
then</del> the deduced type is <code>function&lt;R(A...)&gt;</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2020-02-13; Prague]</i></p>

<p>
LWG improves wording matching Marshall's Mandating paper.
</p>
<p><i>[Status to Immediate on Friday in Prague.]</i></p>



<p id="res-3238"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4849">N4849</a>.</p>

<ol>
<li><p>Modify 22.10.17.3.2 <a href="https://wg21.link/func.wrap.func.con">[func.wrap.func.con]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> This edit should be used instead of the corresponding edit in <a href="https://wg21.link/p1460">P1460</a>]
</p>
</blockquote>

<blockquote>
<pre>
template&lt;class F&gt; function(F) -&gt; function&lt;<i>see below</i>&gt;;
</pre>
<blockquote>
<p>
<ins>-?- <i>Constraints:</i> <code>&amp;F::operator()</code> is well-formed when treated as an unevaluated operand 
and <code>decltype(&amp;F::operator())</code> is of the form <code>R(G::*)(A...) <i>cv</i> &amp;<sub><i>opt</i></sub> 
noexcept<sub><i>opt</i></sub></code> for a class type <code>G</code>.</ins>
<p/>
-12- <i>Remarks:</i> <del>This deduction guide participates in overload resolution only if 
<code>&amp;F::operator()</code> is well-formed when treated as an unevaluated operand. 
In that case, if <code>decltype(&amp;F::operator())</code> is of the form <code>R(G::*)(A...) <i>cv</i> 
&amp;<sub><i>opt</i></sub> noexcept<sub><i>opt</i></sub></code> for a class type <code>G</code>, 
then t</del><ins>T</ins>he deduced type is <code>function&lt;R(A...)&gt;</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>




</body>
</html>
