<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2132: std::function ambiguity</title>
<meta property="og:title" content="Issue 2132: std::function ambiguity">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2132.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++14">C++14</a> status.</em></p>
<h3 id="2132"><a href="lwg-defects.html#2132">2132</a>. <code>std::function</code> ambiguity</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++14">C++14</a>
 <b>Submitter:</b> Ville Voutilainen <b>Opened:</b> 2012-02-28 <b>Last modified:</b> 2016-01-28</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#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Consider the following:
</p>
<blockquote><pre>
#include &lt;functional&gt;

void f(std::function&lt;void()&gt;) {}
void f(std::function&lt;void(int)&gt;) {}

int main() {
  f([]{});
  f([](int){});
}
</pre></blockquote>
<p>
The calls to <code>f</code> in <code>main</code> are ambiguous. Apparently because the
conversion sequences to <code>std::function</code> from the lambdas are identical. 
The standard specifies that the function object given to <code>std::function</code>
"shall be <em>Callable</em> (20.8.11.2) for argument types <code>ArgTypes</code> and 
return type <code>R</code>." It doesn't say that if this is not the case, the 
constructor isn't part of the overload set.
<p/>
Daniel: During the preparation of <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3123.html">N3123</a>
it turned out that there are no longer reasons to refer to <em>INVOKE</em> as a
conceptually entity alone, its real implementation as a function template <code>invoke</code>
is possible but was deferred for a later point in time. Defining a type trait for
the <em>Callable</em> requirement would also be possible, so there seem to be no technical
reasons why the template constructor of <code>std::function</code> should not be
constrained. The below suggested wording does this without introducing a special
trait for this. This corresponds to the way that has been used to specify the
<code>result_of</code> trait. Note that the definition of the <em>Callable</em>
requirement is perfectly suitable for this, because it is a pure syntactically
based requirement and can be directly transformed into a constrained template.
<p/>
The suggested resolution also applies such wording to the "perfectly forwarding"
assignment operator
</p>
<blockquote><pre>
template&lt;class F&gt; function&amp; operator=(F&amp;&amp;);
</pre></blockquote>
<p>
The positive side-effect of this is that it automatically implements a solution to
a problem similar to that mentioned in issue <a href="lwg-defects.html#1234" title="&quot;Do the right thing&quot; and NULL (Status: C++11)">1234</a><sup><a href="https://cplusplus.github.io/LWG/issue1234" title="Latest snapshot">(i)</a></sup>.
<p/>
It would be possible to apply similar constraints to the member signatures
</p>
<blockquote><pre>
template&lt;class F&gt; function&amp; operator=(reference_wrapper&lt;F&gt;);

template&lt;class F, class A&gt; void assign(F&amp;&amp;, const A&amp;);
</pre></blockquote>
<p>
as well. At this point there does not seem to be a pestering reason to do so.
</p>

<p><i>[2012-10 Portland: Move to Review]</i></p>

<p>
STL: This is a real issue, but does not like a resolution relying on a SFINAEable metafunction
that is not specified and available to the users.
</p>

<p>
<code>packaged_task</code> has the same issue.
</p>

<p>
STL strongly wants to see an <code>is_callable</code> type trait to clarify the proposed wording.
</p>

<p>
Jeremiah concerned about holding up what appears to be a correct resolution for a hypothetical
better one later - the issue is real.
</p>

<p>
Why must <code>f</code> by CopyConstructible?  Surely MoveConstructible would be sufficient?
</p>

<p>
Answer: because <code>function</code> is CopyConstructible, and the bound functor is type-erased
so must support all the properties of <code>function</code> itself.
</p>

<p>
Replace various applications of <code>declval</code> in the proposed resolution with simply using
the passed functor object, <code>f</code>.
</p>

<p>
Alisdair to apply similar changes to <code>packaged_task</code>.
</p>

<p><i>[2012-11-09, Vicente J. Botet Escriba provides another example]</i></p>


<p>
Consider the following:
</p>
<blockquote><pre>
class AThreadWrapper {
public:
  explicit operator std::thread();
  ...
};
std::thread th = std::thread(AThreadWrapper); // call to conversion operator intended
</pre></blockquote>
<p>
The call to the conversion operator is overloaded with the thread constructor. But thread constructor requirement 
makes it fail as <code>AThreadWrapper</code> is not a Callable and the compiler tries to instantiate the thread 
constructor and fails.
</p>

<p><i>[2014-02-14 Issaquah meeting: Move to Immediate]</i></p>




<p id="res-2132"><b>Proposed resolution:</b></p>
<p>This wording is relative to N3376.</p>

<ol>
<li><p>Change the following paragraphs in 22.10.17.3.2 <a href="https://wg21.link/func.wrap.func.con">[func.wrap.func.con]</a>:
[<em>Editorial comment</em>: The removal of the seemingly additional no-throw
requirements of copy constructor and destructor of <code>A</code> is recommended,
because they are already part of the Allocator requirements. Similar clean-up
has been suggested by <a href="lwg-defects.html#2070" title="allocate_shared should use allocator_traits&lt;A&gt;::construct (Status: Resolved)">2070</a><sup><a href="https://cplusplus.github.io/LWG/issue2070" title="Latest snapshot">(i)</a></sup> &mdash; <em>end comment</em>]</p>

<blockquote>
<pre>
template&lt;class F> function(F f);
template&lt;class F, class A&gt; function(allocator_arg_t, const A&amp; a, F f);
</pre>
<blockquote>
<p>
-7- <i>Requires</i>: <code>F</code> shall be <code>CopyConstructible</code>. <del><code>f</code> shall be 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>. 
The copy constructor and destructor of <code>A</code> shall not throw exceptions.</del>
<p/>
<ins>-?- <i>Remarks</i>: These constructors shall not participate in overload resolution unless
<code>f</code> is 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>
</p>
</blockquote>
<p>
[&hellip;]
</p>
<pre>
template&lt;class F> function&amp; operator=(F&amp;&amp; f);
</pre>
<blockquote>
<p>
-18- <i>Effects</i>: <code>function(std::forward&lt;F&gt;(f)).swap(*this);</code>
<p/>
-19- <i>Returns</i>: <code>*this</code>
<p/>
<ins>-?- <i>Remarks</i>: This assignment operator shall not participate in overload resolution unless
<code>declval&lt;typename decay&lt;F&gt;::type&amp;&gt;()</code> is 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>
</p>
</blockquote>
</blockquote>

</li>
</ol>






</body>
</html>
