<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 817: bind needs to be moved</title>
<meta property="og:title" content="Issue 817: bind needs to be moved">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue817.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++11">C++11</a> status.</em></p>
<h3 id="817"><a href="lwg-defects.html#817">817</a>. <code>bind</code> needs to be moved</h3>
<p><b>Section:</b> 22.10.15.4 <a href="https://wg21.link/func.bind.bind">[func.bind.bind]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Howard Hinnant <b>Opened:</b> 2008-03-17 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#func.bind.bind">issues</a> in [func.bind.bind].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p><b>Addresses US 72, JP 38 and DE 21</b></p>

<p>
The functor returned by <code>bind()</code> should have a move constructor that
requires only move construction of its contained functor and bound arguments.
That way move-only functors can be passed to objects such as <code>thread</code>.
</p>
<p>
This issue is related to issue <a href="lwg-defects.html#816" title="Should bind()'s returned functor have a nofail copy ctor when bind() is nofail? (Status: Resolved)">816</a><sup><a href="https://cplusplus.github.io/LWG/issue816" title="Latest snapshot">(i)</a></sup>.
</p>

<p>
US 72:
</p>

<blockquote><p>
<code>bind</code> should support move-only functors and bound arguments.
</p></blockquote>

<p>
JP 38:
</p>

<blockquote>
<p>
add the move requirement for bind's return type.
</p>
<p>
For example, assume following <code>th1</code> and <code>th2</code>,
</p>

<blockquote><pre>
void f(vector&lt;int&gt; v) { }

vector&lt;int&gt; v{ ... };
thread th1([v]{ f(v); });
thread th2(bind(f, v));
</pre></blockquote>

<p>
When function object are set to thread, <code>v</code> is moved to <code>th1</code>'s lambda
expression in a Move Constructor of lambda expression because <code>th1</code>'s lambda
expression has a Move Constructor. But <code>bind</code> of <code>th2</code>'s
return type doesn't have the requirement of Move, so it may not
moved but copied.
</p>
<p>
Add the requirement of move to get rid of this useless copy.
</p>
<p>
And also, add the <code>MoveConstructible</code> as well as <code>CopyConstructible</code>.
</p>
</blockquote>

<p>
DE 21
</p>

<blockquote><p>
The specification for bind claims twice that "the values and types for
the bound arguments v1, v2, ..., vN are determined as specified below".
No such specification appears to exist.
</p></blockquote>

<p><i>[
San Francisco:
]</i></p>


<blockquote><p>
Howard to provide wording.
</p></blockquote>

<p><i>[
Post Summit Alisdair and Howard provided wording.
]</i></p>


<blockquote>
<p>
Several issues are being combined in this resolution.  They are all touching the
same words so this is an attempt to keep one issue from stepping on another, and
a place to see the complete solution in one place.
</p>

<ol>
<li>
<code>bind</code> needs to be "moved".
</li>
<li>
22.10.15.4 <a href="https://wg21.link/func.bind.bind">[func.bind.bind]</a>/p3, p6 and p7 were accidently removed from N2798.
</li>
<li>
Issue <a href="lwg-defects.html#929" title="Thread constructor (Status: C++11)">929</a><sup><a href="https://cplusplus.github.io/LWG/issue929" title="Latest snapshot">(i)</a></sup> argues for a way to pass by &amp;&amp; for
efficiency but retain the decaying behavior of pass by value for the
<code>thread</code> constructor.  That same solution is applicable here.
</li>
</ol>
</blockquote>

<p><i>[
Batavia (2009-05):
]</i></p>

<blockquote>
<p>
We were going to recommend moving this issue to Tentatively Ready
until we noticed potential overlap with issue 816 (q.v.).
</p>
<p>
Move to Open,
and recommend both issues be considered together
(and possibly merged).
</p>
</blockquote>

<p><i>[
2009-07 Frankfurt:
]</i></p>


<blockquote><p>
The proposed resolution uses concepts. Leave Open.
</p></blockquote>

<p><i>[
2009-10 Santa Cruz:
]</i></p>


<blockquote><p>
Leave as Open. Howard to provide deconceptified wording.
</p></blockquote>

<p><i>[
2009-11-07 Howard updates wording.
]</i></p>


<p><i>[
2009-11-15 Further updates by Peter, Chris and Daniel.
]</i></p>


<p><i>[
Moved to Tentatively Ready after 6 positive votes on c++std-lib.
]</i></p>



<p id="res-817"><b>Proposed resolution:</b></p>
<p>
Change 22.10 <a href="https://wg21.link/function.objects">[function.objects]</a> p2:
</p>

<blockquote><pre>
template&lt;class F<del>n</del>, class... <del>Types</del> <ins>BoundArgs</ins>&gt;
  <i>unspecified</i> bind(F<del>n</del><ins>&amp;&amp;</ins>, <del>Types</del> <ins>BoundArgs&amp;&amp;</ins>...);
template&lt;class R, class F<del>n</del>, class... <del>Types</del> <ins>BoundArgs</ins>&gt;
  <i>unspecified</i> bind(F<del>n</del><ins>&amp;&amp;</ins>, <del>Types</del> <ins>BoundArgs&amp;&amp;</ins>...);
</pre></blockquote>

<p>
Change 22.10.4 <a href="https://wg21.link/func.require">[func.require]</a>:
</p>

<blockquote>
<p>
4 Every call wrapper (22.10.3 <a href="https://wg21.link/func.def">[func.def]</a>) shall be
<code><del>Copy</del><ins>Move</ins>Constructible</code>. A <i>simple call
wrapper</i> is a call wrapper that is <ins><code>CopyConstructible</code> and</ins>
<code>CopyAssignable</code> and whose copy constructor<ins>, move constructor</ins> and assignment operator do not
throw exceptions. A <i>forwarding call wrapper</i> is a call wrapper that can be
called with an argument list. [<i>Note:</i> in a typical implementation
forwarding call wrappers have an overloaded function call operator of the form
</p>
<blockquote><pre>
template&lt;class... <del>ArgTypes</del><ins>UnBoundsArgs</ins>&gt;
R operator()(<del>ArgTypes</del><ins>UnBoundsArgs</ins>&amp;&amp;... <ins>unbound_</ins>args) cv-qual;
</pre></blockquote>
<p>
&mdash; <i>end note</i>]
</p>
</blockquote>

<p>
Change 22.10.15.4 <a href="https://wg21.link/func.bind.bind">[func.bind.bind]</a>:
</p>

<blockquote>
<p><ins>
Within this clause:
</ins></p>

<ul>
<li><ins>
Let <code>FD</code> be a synonym for the type <code>decay&lt;F&gt;::type</code>.
</ins></li>
<li><ins>
Let <code>fd</code> be an lvalue of type <code>FD</code> constructed from
<code>std::forward&lt;F&gt;(f)</code>.
</ins></li>
<li><ins>
Let <code>Ti</code> be a synonym for the i<sup><i>th</i></sup> type in the
template parameter pack <code>BoundArgs</code>.
</ins></li>
<li><ins>
Let <code>TiD</code> be a synonym for the type <code>decay&lt;Ti&gt;::type</code>.
</ins></li>
<li><ins>
Let <code>ti</code> be the i<sup><i>th</i></sup> argument in the function parameter
pack <code>bound_args</code>.
</ins></li>
<li><ins>
Let <code>tid</code> be an lvalue of type <code>TiD</code> constructed from
<code>std::forward&lt;Ti&gt;(ti)</code>.
</ins></li>
<li><ins>
Let <code>Uj</code> be the j<sup><i>th</i></sup> deduced type of the <code>UnBoundArgs&amp;&amp;...</code>
parameter of the <code>operator()</code> of the forwarding call wrapper.
</ins></li>
<li><ins>
Let <code>uj</code> be the j<sup><i>th</i></sup> argument associated with <code>Uj</code>.
</ins></li>
</ul>

<pre>
template&lt;class F, class... BoundArgs&gt;
  <i>unspecified</i> bind(F<ins>&amp;&amp;</ins> f, BoundArgs<ins>&amp;&amp;</ins>... bound_args);
</pre>

<blockquote>
<p>
-1- <i>Requires:</i>
<ins><code>is_constructible&lt;FD, F&gt;::value</code>
shall be <code>true</code>.</ins>
<ins>For each <code>Ti</code> in <code>BoundArgs</code>,
<code>is_constructible&lt;TiD, Ti&gt;::value</code> shall be
<code>true</code></ins>.
<del><code>F</code> and each <code>Ti</code> in
<code>BoundArgs</code> shall be CopyConstructible.</del>
<code><i>INVOKE</i>(f<ins>d</ins>, w1, w2, ..., wN)</code> (22.10.4 <a href="https://wg21.link/func.require">[func.require]</a>) shall be a valid expression for some values
<i>w1, w2, ..., wN</i>, where <code>N == sizeof...(bound_args)</code>.
</p>
<p>
-2- <i>Returns:</i> A forwarding call wrapper <code>g</code> with a weak
result type (22.10.4 <a href="https://wg21.link/func.require">[func.require]</a>). The effect of <code>g(u1, u2,
..., uM)</code> shall be <code><i>INVOKE</i>(f<ins>d</ins>, v1, v2, ..., vN,
result_of&lt;F<ins>D</ins> <i>cv</i> (V1, V2, ..., VN)&gt;::type)</code>, where
<i>cv</i> represents the <i>cv</i>-qualifiers of <code>g</code> and the
values and types of the bound arguments <code>v1, v2, ..., vN</code> are
determined as specified below.
<ins>The copy constructor and move constructor of the forwarding call wrapper shall throw an
exception if and only if the corresponding constructor of <code>FD</code> or of any of the types
<code>TiD</code> throws an exception.</ins>
</p>
<p>
-3- <i>Throws:</i> Nothing unless the <del>copy</del>
construct<ins>ion</ins><del>or</del> of
<code><del>F</del><ins>fd</ins></code> or of one of the <ins>values
<code>tid</code></ins> <del>types in the <code>BoundArgs...</code> pack
expansion</del> throws an exception.
</p>
<p>
<ins>
<i>Remarks:</i> The <i>unspecified</i> return type shall satisfy the
requirements of <code>MoveConstructible</code>.  If all of <code>FD</code> and
<code>TiD</code> satisfy the requirements of <code>CopyConstructible</code> then
the <i>unspecified</i> return type shall satisfy the requirements of
<code>CopyConstructible</code>. [<i>Note:</i> This implies that all of
<code>FD</code> and <code>TiD</code> shall be <code>MoveConstructible</code> &mdash;
<i>end note</i>]
</ins>
</p>
</blockquote>

<pre>
template&lt;class R, class F, class... BoundArgs&gt;
  <i>unspecified</i> bind(F<ins>&amp;&amp;</ins> f, BoundArgs<ins>&amp;&amp;</ins>... bound_args);
</pre>

<blockquote>
<p>
-4- <i>Requires:</i>
<ins><code>is_constructible&lt;FD, F&gt;::value</code>
shall be <code>true</code>.</ins>
<ins>For each <code>Ti</code> in <code>BoundArgs</code>,
<code>is_constructible&lt;TiD, Ti&gt;::value</code> shall be
<code>true</code></ins>.
<del><code>F</code> and each <code>Ti</code> in
<code>BoundArgs</code> shall be CopyConstructible.</del>
<code><i>INVOKE</i>(f<ins>d</ins>, w1,
w2, ..., wN)</code> shall be a valid expression for some values <i>w1, w2,
..., wN</i>, where <code>N == sizeof...(bound_args)</code>.
</p>
<p>
-5- <i>Returns:</i> A forwarding call wrapper <code>g</code> with a nested
type <code>result_type</code> defined as a synonym for <code>R</code>. The
effect of <code>g(u1, u2, ..., uM)</code> shall be <code><i>INVOKE</i>(f<ins>d</ins>, v1,
v2, ..., vN, R)</code>, where the values and types of the bound arguments
<code>v1, v2, ..., vN</code> are determined as specified below.
<ins>The copy constructor and move constructor of the forwarding call wrapper shall throw an
exception if and only if the corresponding constructor of <code>FD</code> or of any of the types
<code>TiD</code> throws an exception.</ins>
</p>
<p>
-6- <i>Throws:</i> Nothing unless the <del>copy</del>
construct<ins>ion</ins><del>or</del> of
<code><del>F</del><ins>fd</ins></code> or of one of the <ins>values
<code>tid</code></ins> <del>types in the <code>BoundArgs...</code> pack
expansion</del> throws an exception.
</p>
<p>
<ins>
<i>Remarks:</i> The <i>unspecified</i> return type shall satisfy the
requirements of <code>MoveConstructible</code>.  If all of <code>FD</code> and
<code>TiD</code> satisfy the requirements of <code>CopyConstructible</code> then
the <i>unspecified</i> return type shall satisfy the requirements of
<code>CopyConstructible</code>. [<i>Note:</i> This implies that all of
<code>FD</code> and <code>TiD</code> shall be <code>MoveConstructible</code> &mdash;
<i>end note</i>]
</ins>
</p>
</blockquote>

<p>
-7- The values of the <i>bound arguments</i> <code>v1, v2, ..., vN</code> and
their corresponding types <code>V1, V2, ..., VN</code> depend on the type<ins>s
<code>TiD</code> derived from</ins>
<del>of the corresponding argument <code>ti</code> in <code>bound_args</code> of type
<code>Ti</code> in <code>BoundArgs</code> in</del>
the call to <code>bind</code> and the
<i>cv</i>-qualifiers <i>cv</i> of the call wrapper <code>g</code> as
follows:
</p>

<ul>
<li>
if <code><del>ti</del> <ins>TiD</ins></code> is <del>of type</del>
<code>reference_wrapper&lt;T&gt;</code> the argument is
<code>ti<ins>d</ins>.get()</code> and its type <code>Vi</code> is <code>T&amp;</code>;
</li>
<li>
if the value of
<code><del>std::</del>is_bind_expression&lt;Ti<ins>D</ins>&gt;::value</code> is
<code>true</code> the argument is <code>ti<ins>d</ins>(<ins>std::forward&lt;Uj&gt;(uj)...</ins> <del>u1, u2, ..., uM</del>)</code>
and its type <code>Vi</code> is <code>result_of&lt;Ti<ins>D</ins> <i>cv</i>
(<ins>Uj...</ins> <del>U1&amp;, U2&amp;, ..., UM&amp;</del>)&gt;::type</code>;
</li>
<li>
if the value <code>j</code> of
<code><del>std::</del>is_placeholder&lt;Ti<ins>D</ins>&gt;::value</code> is not zero
the argument is <code>std::forward&lt;Uj&gt;(uj)</code> and its type
<code>Vi</code> is <code>Uj&amp;&amp;</code>;
</li>
<li>
otherwise the value is <code>ti<ins>d</ins></code> and its type <code>Vi</code>
is <code>Ti<ins>D</ins> <i>cv</i> &amp;</code>.
</li>
</ul>

</blockquote>






</body>
</html>
