<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2850: std::function move constructor does unnecessary work</title>
<meta property="og:title" content="Issue 2850: std::function move constructor does unnecessary work">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2850.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++17">C++17</a> status.</em></p>
<h3 id="2850"><a href="lwg-defects.html#2850">2850</a>. <code>std::function</code> move constructor does unnecessary work</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++17">C++17</a>
 <b>Submitter:</b> Geoffrey Romer <b>Opened:</b> 2016-12-15 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>0
</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++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Consider [func.wrap.func.con]/p5:
</p>
<blockquote><pre>
function(function&amp;&amp; f);
</pre>
<blockquote>
<p>
<i>Effects:</i> If <code>!f</code>, <code>*this</code> has no target; otherwise, move constructs the target of <code>f</code> into the target 
of <code>*this</code>, leaving <code>f</code> in a valid state with an unspecified value.
</p>
</blockquote>
</blockquote>
<p>
By my reading, this wording requires the move constructor of <code>std::function</code> to construct an entirely new target object. 
This is silly: in cases where the target is held in separately allocated memory (i.e. where the target doesn't fit in 
<code>std::function</code>'s internal buffer, if any), <code>std::function</code>'s move constructor can be implemented by simply 
transferring ownership of the existing target object (which is a simple pointer assignment), so this requirement forces an 
unnecessary constructor invocation and probably an unnecessary allocation (the latter can be avoided with something like 
double-buffering, but ew). Fixing this would technically be a visible change, but I have a hard time imagining reasonable 
code that would be broken by it, especially since both libstdc++ and libc++ already do the sensible thing, constructing a 
new target only if the target is held in an internal buffer, and otherwise assigning pointers.
</p>

<p><i>[2017-01-27 Telecon]</i></p>

<p>Priority 0</p>


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

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

<blockquote class="note">
<p>
<i>Drafting note:</i> The "equivalent to ... before the construction" wording is based on the wording for <code>MoveConstructible</code>.
</p>
</blockquote>

<blockquote>
<pre>
function(function&amp;&amp; f);
</pre>
<blockquote>
<p>
-5- <i><del>Effects</del><ins>Postconditions</ins>:</i> If <code>!f</code>, <code>*this</code> has no target; otherwise, <del>move 
constructs the target of <code>f</code> into the target of <code>*this</code>, leaving <code>f</code></del><ins>the target of <code>*this</code> 
is equivalent to the target of <code>f</code> before the construction, and <code>f</code> is</ins> in a valid state with an 
unspecified value.
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
