<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3493: The constructor of std::function taking an F is missing a constraint</title>
<meta property="og:title" content="Issue 3493: The constructor of std::function taking an F is missing a constraint">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3493.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#New">New</a> status.</em></p>
<h3 id="3493"><a href="lwg-active.html#3493">3493</a>. The constructor of <code>std::function</code> taking an <code>F</code> is missing a constraint</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#New">New</a>
 <b>Submitter:</b> Ville Voutilainen <b>Opened:</b> 2020-10-31 <b>Last modified:</b> 2021-08-20</p>
<p><b>Priority: </b>3
</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#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In <a href="https://wg21.link/p0288">P0288</a>, <code>any_invocable</code> is (correctly) constraining
its constructor that takes an <code>F</code>:
</p>
<blockquote>
<pre>
template&lt;class F&gt; any_invocable(F&amp;&amp; f);
</pre>
<blockquote>
<p>
Let <code>VT</code> be <code>decay_t&lt;F&gt;</code>.
<p/>
<i>Constraints:</i>
</p>
<ol style="list-style-type: none">
<li><p>&mdash; [&hellip;]</p></li>
<li><p>&mdash; <code>is_constructible_v&lt;VT, F&gt;</code> is <code>true</code>, and</p></li>
<li><p>&mdash; [&hellip;]</p></li>
</ol>
</blockquote>
</blockquote>
<p>
<code>std::function</code> doesn't do that. According to <a href="https://wg21.link/n4868">N4868</a>,
22.10.17.3.2 <a href="https://wg21.link/func.wrap.func.con">[func.wrap.func.con]</a> p8 has a constraint for Lvalue-Callable, but not for
copy-constructibility. There is a precondition in p9, but that's not enough for portable
well/ill-formedness.
<p/>
Since this is a constructor, and we want to give the right answer to
<code>is_constructible</code>/<code>constructible_from</code> queries, we should
add the relevant constraint.
</p>

<p><i>[2020-11-01; Daniel comments]</i></p>

<p>
This issue has some overlap with LWG <a href="lwg-defects.html#2774" title="std::function construction vs assignment (Status: C++23)">2774</a><sup><a href="https://cplusplus.github.io/LWG/issue2774" title="Latest snapshot">(i)</a></sup>.
</p>

<p><i>[2021-01-15; Telecon prioritization]</i></p>

<p>
Set priority to 3 following reflector and telecon discussions.
</p>

<p><i>[2021-05-17; Tim comments]</i></p>

<p>
The new constraint causes constraint recursion in an example like:
</p>
<blockquote>
<pre>
struct C {
    explicit C(std::function&lt;void()&gt;); // #1
    void operator()() {}
};
static_assert(std::is_constructible_v&lt;C, const C&amp;&gt;);
</pre>
</blockquote>
<p>
Here, to determine whether a <code>C</code> can be constructed from a <code>const C</code>
lvalue, the overload resolution will attempt to determine whether the constructor
marked <code>#1</code> is a viable candidate, which involves a determination of
whether that lvalue can be implicitly converted to a <code>std::function&lt;void()&gt;</code>,
which, with the new constraint, requires a determination whether
<code>C</code> is copy-constructible &mdash; in other words, whether it can be constructed
from a <code>C</code> lvalue.
<p/>
This is similar to LWG <a href="lwg-defects.html#3420" title="cpp17-iterator should check that the type looks like an iterator first (Status: C++23)">3420</a><sup><a href="https://cplusplus.github.io/LWG/issue3420" title="Latest snapshot">(i)</a></sup>: in both cases we have a class
(<code>filesystem::path</code> there, <code>function</code> here) that is
convertible from every type that are, <i>inter alia</i>, copy constructible,
and this then results in constraint recursion when we ask whether a different
type that is constructible from such a class is copy constructible.
<p/>
The <code>C</code> above is reduced from an internal helper type in libstdc++. Given
the ubiquity of call wrappers &mdash; types that are callable in their own right
and therefore may not be able to be ruled out by the Lvalue-Callable constraint,
and can also naturally have a constructor that take the wrapped function object
as the argument, triggering the recursion scenario &mdash; it is not clear that
there is a good way to add this constraint without causing undue breakage.
</p>

<p><i>[2021-08-20; LWG telecon]</i></p>

<p>
LWG requested that the constraint cited above for
<code>move_only_function</code> (n&eacute; <code>any_invocable</code>)
be moved to a <i>Mandates:</i> element instead, to avoid the same
constraint recursion.
</p>



<p id="res-3493"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4868">N4868</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 f);
</pre>
<blockquote>
<p>
-8- <i>Constraints:</i> <code>F</code> is Lvalue-Callable (22.10.17.3.1 <a href="https://wg21.link/func.wrap.func.general">[func.wrap.func.general]</a>) for
argument types <code>ArgTypes...</code> and return type <code>R</code><ins>, and
<code>is_copy_constructible_v&lt;F&gt;</code> is <code>true</code></ins>.
<p/>
-9- <i>Preconditions:</i> <code>F</code> meets the <i>Cpp17CopyConstructible</i> requirements.
<p/>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
