<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2389: [fund.ts] function::operator= is over-specified and handles allocators incorrectly</title>
<meta property="og:title" content="Issue 2389: [fund.ts] function::operator= is over-specified and handles allocators incorrectly">
<meta property="og:description" content="C++ library issue. Status: TS">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2389.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#TS">TS</a> status.</em></p>
<h3 id="2389"><a href="lwg-defects.html#2389">2389</a>. [fund.ts] <code>function::operator=</code> is over-specified and handles allocators incorrectly</h3>
<p><b>Section:</b> 4.2.1 <a href="https://cplusplus.github.io/fundamentals-ts/v1.html#func.wrap.func.con">[fund.ts::func.wrap.func.con]</a> <b>Status:</b> <a href="lwg-active.html#TS">TS</a>
 <b>Submitter:</b> Pablo Halpern <b>Opened:</b> 2014-05-23 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>2
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#TS">TS</a> status.</p>
<p><b>Discussion:</b></p>
<p><b>Addresses: fund.ts</b></p>

<p>
This issue against the TS is similar to LWG <a href="lwg-closed.html#2386" title="function::operator= handles allocators incorrectly (Status: NAD)">2386</a><sup><a href="https://cplusplus.github.io/LWG/issue2386" title="Latest snapshot">(i)</a></sup>, which is against the standard. The Effects clauses for the assignment 
operator for class template <code>function</code> are written as code that constructs a temporary <code>function</code> and then swaps it 
with <code>*this</code>. 
The intention appears to be that assignment should have the strong exception guarantee, i.e., <code>*this</code> is not modified if 
an exception is thrown. The description in the standard is incorrect when <code>*this</code> was originally constructed using an 
allocator. The TS attempts to correct the problem, but the correction is incomplete.
<p/>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3908.html#mods.func.wrap">The wording in the TS</a> uses 
<code>get_memory_resource()</code> to construct a temporary <code>function</code> object with the same allocator as the left-hand size 
(lhs) of the assignment. The intended result of using this pattern was that the allocator for <code>*this</code> would be unchanged, 
but it doesn't quite work. The problem is that the allocator returned by <code>get_memory_resource()</code> is not the same type as 
the type-erased allocator used to construct the <code>function</code> object, but rather a type-erased distillation of that type that 
is insufficient for making a true copy of the allocator. The rules for type-erased allocators in the TS 
(<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3908.html#memory.type.erased.allocator">[memory.type.erased.allocator]</a>) 
specify that the lifetime of the object returned by <code>get_memory_resource()</code> is sometimes tied to the lifetime of <code>*this</code>, 
which might cause the (single copy of) the allocator to be destroyed if the <code>swap</code> operation destroys and reconstructs <code>*this</code>, 
as some implementations do (and are allowed to do).
<p/>
The desired behavior is that assignment would leave the allocator of the lhs unchanged. The way to achieve this behavior is to 
construct the temporary <code>function</code> using the original allocator. Unfortunately, we cannot describe the desired behavior in 
pure code, because <code>get_memory_resource()</code> does not really name the type-erased allocator, as mentioned above. The PR below, 
therefore, uses pseudo-code, inventing a fictitious <code><i>ALLOCATOR_OF</i>(f)</code> expression that evaluates to the actual allocator 
type, even if that allocator was type erased. I have implemented this PR successfully.
</p>

<p><i>[2014-06-21, Rapperswil]</i></p>

<p>
Apply to Library Fundamentals TS (after removing the previous "Throws: Nothing" element to prevent an editorial conflict with
<a href="lwg-defects.html#2401" title="std::function needs more noexcept (Status: C++17)">2401</a><sup><a href="https://cplusplus.github.io/LWG/issue2401" title="Latest snapshot">(i)</a></sup>).
</p>


<p id="res-2389"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3908.html">N3908</a>.</p>

<ol>
<li><p>Change in [mods.func.wrap] in the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3908.html#mods.func.wrap">Library TS</a> 
as indicated:
</p>
<blockquote>
<p>
<ins>In the following descriptions, let <code><i>ALLOCATOR_OF</i>(f)</code> be the allocator specified in the construction of <code>function</code> 
<code>f</code>, or <code>allocator&lt;char&gt;()</code> if no allocator was specified.</ins>
</p>
<pre>
function&amp; operator=(const function&amp; f);
</pre>
<blockquote>
<p>
-5- <i>Effects</i>: <code>function(allocator_arg, <del>get_memory_resource()</del><ins><i>ALLOCATOR_OF</i>(*this)</ins>, f).swap(*this);</code>
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
function&amp; operator=(function&amp;&amp; f);
</pre>
<blockquote>
<p>
-8- <i>Effects</i>: <code>function(allocator_arg, <del>get_memory_resource()</del><ins><i>ALLOCATOR_OF</i>(*this)</ins>, std::move(f)).swap(*this);</code>
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
function&amp; operator=(nullptr_t);
</pre>
<blockquote>
<p>
-11- <i>Effects</i>: If <code>*this != NULL</code>, destroys the target of <code>this</code>.
<p/>
-12- <i>Postconditions</i>: <code>!(*this)</code>. <ins>The memory resource returned by <code>get_memory_resource()</code> after the assignment 
is equivalent to the memory resource before the assignment. [<i>Note</i>: the address returned by <code>get_memory_resource()</code> 
might change &mdash; <i>end note</i>]</ins>
<p/>
-13- <i>Returns</i>: <code>*this</code>  
</p>
</blockquote>
<pre>
template&lt;class F&gt; function&amp; operator=(F&amp;&amp; f);
</pre>
<blockquote>
<p>
-15- <i>Effects</i>: <code>function(allocator_arg, <del>get_memory_resource()</del><ins><i>ALLOCATOR_OF</i>(*this)</ins>, 
std::forward&lt;F&gt;(f)).swap(*this);</code>
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class F&gt; function&amp; operator=(reference_wrapper&lt;F&gt; f);
</pre>
<blockquote>
<p>
-18- <i>Effects</i>: <code>function(allocator_arg, <del>get_memory_resource()</del><ins><i>ALLOCATOR_OF</i>(*this)</ins>, f).swap(*this);</code>
<p/>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
