<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2839: Self-move-assignment of library types, again</title>
<meta property="og:title" content="Issue 2839: Self-move-assignment of library types, again">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2839.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++23">C++23</a> status.</em></p>
<h3 id="2839"><a href="lwg-defects.html#2839">2839</a>. Self-move-assignment of library types, again</h3>
<p><b>Section:</b> 16.4.6.17 <a href="https://wg21.link/lib.types.movedfrom">[lib.types.movedfrom]</a>, 16.4.5.9 <a href="https://wg21.link/res.on.arguments">[res.on.arguments]</a>, 23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Tim Song <b>Opened:</b> 2016-12-09 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>2
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
LWG <a href="lwg-defects.html#2468" title="Self-move-assignment of library types (Status: C++17)">2468</a><sup><a href="https://cplusplus.github.io/LWG/issue2468" title="Latest snapshot">(i)</a></sup>'s resolution added to <code>MoveAssignable</code> the requirement to tolerate self-move-assignment,
but that does nothing for library types that aren't explicitly specified to meet <code>MoveAssignable</code> other than make
those types not meet <code>MoveAssignable</code> any longer.
<p/>
To realize the intent here, we need to carve out an exception to 16.4.5.9 <a href="https://wg21.link/res.on.arguments">[res.on.arguments]</a>'s restriction for
move assignment operators and specify that self-move-assignment results in valid but unspecified state unless otherwise
specified. The proposed wording below adds that to 16.4.6.17 <a href="https://wg21.link/lib.types.movedfrom">[lib.types.movedfrom]</a> since it seems to fit well with the
theme of the current paragraph in that section.
<p/> In addition, to address the issue with 23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a> noted in LWG
<a href="lwg-defects.html#2468" title="Self-move-assignment of library types (Status: C++17)">2468</a><sup><a href="https://cplusplus.github.io/LWG/issue2468" title="Latest snapshot">(i)</a></sup>'s discussion, the requirement tables in that subclause will need to be edited in a way similar to
LWG <a href="lwg-defects.html#2468" title="Self-move-assignment of library types (Status: C++17)">2468</a><sup><a href="https://cplusplus.github.io/LWG/issue2468" title="Latest snapshot">(i)</a></sup>.
</p>

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

<p>Priority 2</p>

<p><i>[2018-1-26 issues processing telecon]</i></p>

<p>Status to 'Open'; Howard to reword using 'MoveAssignable'.</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4618">N4618</a>.</p>

<ol>
<li><p>Add a new paragraph at the end of 16.4.6.17 <a href="https://wg21.link/lib.types.movedfrom">[lib.types.movedfrom]</a>:</p>
<blockquote>
<p>
-1- Objects of types defined in the C++ standard library may be moved from (12.8). Move operations may be
explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects shall be
placed in a valid but unspecified state.
<p/>
<ins>-?- An object of a type defined in the C++ standard library may be move-assigned (11.4.6 <a href="https://wg21.link/class.copy.assign">[class.copy.assign]</a>)
to itself. Such an assignment places the object in a valid but unspecified state unless otherwise specified.</ins>
</p>
</blockquote>
</li>

<li><p>Add a note at the end of 16.4.5.9 <a href="https://wg21.link/res.on.arguments">[res.on.arguments]</a>/1, bullet 3, as indicated:</p>
<blockquote>
<p>
-1- Each of the following applies to all arguments to functions defined in the C++ standard library, unless
explicitly stated otherwise.
</p>
<ol style="list-style-type: none">
<li><p>(1.1) &mdash; [&hellip;]</p></li>
<li><p>(1.2) &mdash; [&hellip;]</p></li>
<li><p>(1.3) &mdash; If a function argument binds to an rvalue reference parameter, the implementation may
assume that this parameter is a unique reference to this argument. [<i>Note:</i> If the parameter is a generic parameter
of the form <code>T&amp;&amp;</code> and an lvalue of type <code>A</code> is bound, the argument binds to an lvalue reference
(14.8.2.1) and thus is not covered by the previous sentence. &mdash; <i>end note</i>] [<i>Note:</i> If a program
casts an lvalue to an xvalue while passing that lvalue to a library function (e.g. by calling the function with the argument
<code>std::move(x)</code>), the program is effectively asking that function to treat that lvalue as a temporary.
The implementation is free to optimize away aliasing checks which might be needed if the argument
was an lvalue. &mdash; <i>end note</i>] <ins>[<i>Note:</i> This does not apply to the argument passed to a
move assignment operator (16.4.6.17 <a href="https://wg21.link/lib.types.movedfrom">[lib.types.movedfrom]</a>). &mdash; <i>end note</i>]</ins></p></li>
</ol>
</blockquote>
</li>

<li><p>Edit Table 83 "Container requirements" in 23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a> as indicated:</p>
<blockquote>
<table border="1">
<caption>Table 83 &mdash; Container requirements</caption>
<tr>
<th>Expression</th>
<th>Return type</th>
<th>Operational<br/>semantics</th>
<th>Assertion&#47;note<br/>pre-&#47;post-condition</th>
<th>Complexity</th>
</tr>

<tr>
<td colspan="5" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td><code>a = rv</code></td>
<td><code>T&amp;</code></td>
<td>All existing elements of <code>a</code><br/>
are either move<br/>
assigned to or<br/>
destroyed</td>
<td><ins>post: If <code>a</code> and <code>rv</code> do not refer to the same object,</ins><br/>
<code>a</code> shall be equal to the value that<br/>
<code>rv</code> had before this assignment</td>
<td>linear</td>
</tr>

<tr>
<td colspan="5" align="center">
<code>&hellip;</code>
</td>
</tr>

</table>
</blockquote>
</li>

<li><p>Edit Table 86 "Allocator-aware container requirements" in 23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a> as indicated:</p>
<blockquote>
<table border="1">
<caption>Table 86 &mdash;  Allocator-aware container requirements</caption>
<tr>
<th>Expression</th>
<th>Return type</th>
<th>Assertion&#47;note<br/>pre-&#47;post-condition</th>
<th>Complexity</th>
</tr>
<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>
<tr>
<td><code>a = rv</code></td>
<td><code>T&amp;</code></td>
<td>
<i>Requires</i>: If <code>allocator_traits&lt;allocator_type<br/>
&gt;::propagate_on_container_move_assignment::value</code><br/>
is <code>false</code>, <code>T</code> is <code>MoveInsertable</code><br/>
into <code>X</code> and <code>MoveAssignable</code>.<br/>
All existing elements of <code>a</code> are either<br/>
move assigned to or destroyed.<br/>
post: <ins>If <code>a</code> and <code>rv</code> do not refer<br/>
to the same object,</ins> <code>a</code> shall be equal<br/>
to the value that <code>rv</code> had before this assignment</td>
<td>linear</td>
</tr>
<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>
</table>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2018-08-16, Howard comments and provides updated wording]</i></p>

<p>
I agreed to provide proposed wording for LWG 2839 that was reworded to use <code>MoveAssignable</code>. The advantage of
this is that <code>MoveAssignable</code> specifies the self-assignment case, thus we do not need to repeat ourselves.
</p>

<p><i>[2018-08-23 Batavia Issues processing]</i></p>

<p>Howard and Tim to discuss a revised P/R.</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4762">N4762</a>.</p>
<ol>
<li><p>Add a new subsection to 16.4.6 <a href="https://wg21.link/conforming">[conforming]</a> after 16.4.6.5 <a href="https://wg21.link/member.functions">[member.functions]</a>:</p>
<blockquote>
<p>
<ins><b>Special members [conforming.special]</b></ins>
<p/>
<ins>Class types defined by the C++ standard library and specified to be default constructible, move constructible,
copy constructible, move assignable, copy assignable, or destructible, shall meet the associated requirements
<i>Cpp17DefaultConstructible</i>, <i>Cpp17MoveConstructible</i>, <i>Cpp17CopyConstructible</i>,
<i>Cpp17MoveAssignable</i>, <i>Cpp17CopyAssignable</i>, and <i>Cpp17Destructible</i>, respectively
(16.4.4.2 <a href="https://wg21.link/utility.arg.requirements">[utility.arg.requirements]</a>).</ins>
</p>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2020-06-06 Tim restores and updates P/R following 2020-05-29 telecon discussion]</i></p>

<p>The standard doesn't define phrases like "default constructible" used in the
previous P/R. Moreover, the library provides a variety of wrapper types, and
whether these types meet the semantic requirements of <i>Cpp17Meowable</i>
(and maybe even syntactic, depending on how "copy constructible" is interpreted)
depends on the property of their underlying wrapped types, which might not even
be an object type (e.g., <code>tuple</code> or <code>pair</code> of references). This is
a large can of worms (see LWG <a href="lwg-active.html#2146" title="Are reference types CopyConstructible&#47;MoveConstructible&#47;CopyAssignable&#47;MoveAssignable&#47;Destructible? (Status: Open)">2146</a><sup><a href="https://cplusplus.github.io/LWG/issue2146" title="Latest snapshot">(i)</a></sup>) that we don't want to get into.
<p/>
There is a suggestion in the telecon to blanket-exempt move-assignment operators from
the 16.4.5.9 <a href="https://wg21.link/res.on.arguments">[res.on.arguments]</a> 1.3 requirement. The revised wording below
does not do so, as that would carve out not just self-move-assignment but also
other aliasing scenarios in which the target object owns the source object.
Whether such scenarios should be permitted is outside the scope of this issue,
though notably <code>assignable_from</code> (18.4.8 <a href="https://wg21.link/concept.assignable">[concept.assignable]</a>)
contains a note alluding to these cases and suggesting that they should be
considered to be outside the domain of <code>=</code> entirely.
</p>

<p><i>[2020-07-17; issue processing telecon]</i></p>

<p>
LWG reviewed the latest proposed resolution.
Unanimous consent to move to Ready.
</p>
<p><i>[2020-11-09 Approved In November virtual meeting. Status changed: Ready &rarr; WP.]</i></p>



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

<ol>
<li><p>Add a new paragraph at the end of 16.4.6.17 <a href="https://wg21.link/lib.types.movedfrom">[lib.types.movedfrom]</a>:</p>
<blockquote>
<p>
-1- Objects of types defined in the C++ standard library may be moved from
( [clss.copy.ctor]). Move operations may be explicitly specified or
implicitly generated. Unless otherwise specified, such moved-from objects shall
be placed in a valid but unspecified state.
<p/>
<ins>-?- An object of a type defined in the C++ standard library may be
move-assigned (11.4.6 <a href="https://wg21.link/class.copy.assign">[class.copy.assign]</a>) to itself. Unless otherwise
specified, such an assignment places the object in a valid but unspecified state.</ins>
</p>
</blockquote>
</li>

<li><p>Edit 16.4.5.9 <a href="https://wg21.link/res.on.arguments">[res.on.arguments]</a>/1, bullet 3, as indicated:</p>
<blockquote>
<p>
-1- Each of the following applies to all arguments to functions defined in the C++ standard library, unless
explicitly stated otherwise.
</p>
<ol style="list-style-type: none">
<li><p>(1.1) &mdash; [&hellip;]</p></li>
<li><p>(1.2) &mdash; [&hellip;]</p></li>
<li><p>(1.3) &mdash; If a function argument binds to an rvalue reference parameter, the implementation may
assume that this parameter is a unique reference to this argument<ins>, except that the argument passed to a
move-assignment operator may be a reference to <code>*this</code> (16.4.6.17 <a href="https://wg21.link/lib.types.movedfrom">[lib.types.movedfrom]</a>)</ins>.
[<i>Note:</i> If the <ins>type of a</ins> parameter is a <del>generic parameter
of the form <code>T&amp;&amp;</code> and an lvalue of type <code>A</code> is bound, the argument binds to an lvalue reference
(13.10.3.2 <a href="https://wg21.link/temp.deduct.call">[temp.deduct.call]</a>) and thus is not covered by the previous sentence.</del>
<ins>forwarding reference (13.10.3.2 <a href="https://wg21.link/temp.deduct.call">[temp.deduct.call]</a>) that is deduced to an lvalue reference type,
then the argument is not bound to an rvalue reference.</ins> &mdash; <i>end note</i>] [<i>Note:</i> If a program
casts an lvalue to an xvalue while passing that lvalue to a library function (e.g. by calling the function with the argument
<code>std::move(x)</code>), the program is effectively asking that function to treat that lvalue as a temporary.
The implementation is free to optimize away aliasing checks which might be needed if the argument
was an lvalue. &mdash; <i>end note</i>]</p></li>
</ol>
</blockquote>
</li>

<li><p>Edit Table 73 "Container requirements" in 23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a> as indicated:</p>
<blockquote>
<table border="1">
<caption>Table 73 &mdash; Container requirements</caption>
<tr>
<th>Expression</th>
<th>Return type</th>
<th>Operational<br/>semantics</th>
<th>Assertion&#47;note<br/>pre-&#47;post-condition</th>
<th>Complexity</th>
</tr>

<tr>
<td colspan="5" align="center">
<code>&hellip;</code>
</td>
</tr>

<tr>
<td><code>a = rv</code></td>
<td><code>T&amp;</code></td>
<td>All existing elements of <code>a</code> are either move assigned to or destroyed</td>
<td><i>Postconditions:</i>
<ins>If <code>a</code> and <code>rv</code> do not refer to the same object,</ins>
<code>a</code> is equal to the value that <code>rv</code> had before this assignment<ins>.</ins></td>
<td>linear</td>
</tr>

<tr>
<td colspan="5" align="center">
<code>&hellip;</code>
</td>
</tr>

</table>
</blockquote>
</li>

<li><p>Edit Table 76 "Allocator-aware container requirements" in 23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a> as indicated:</p>
<blockquote>
<table border="1">
<caption>Table 86 &mdash;  Allocator-aware container requirements</caption>
<tr>
<th>Expression</th>
<th>Return type</th>
<th>Assertion&#47;note<br/>pre-&#47;post-condition</th>
<th>Complexity</th>
</tr>
<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>
<tr>
<td><code>a = rv</code></td>
<td><code>T&amp;</code></td>
<td>
<i>Preconditions</i>: If <code>allocator_traits&lt;allocator_type&gt;<br/>
::propagate_on_container_move_assignment::value</code> is <code>false</code>,<br/>
<code>T</code> is <i>Cpp17MoveInsertable</i>
into <code>X</code> and <i>Cpp17MoveAssignable</i>.<br/>
<i>Effects:</i> All existing elements of <code>a</code> are either
move assigned to or destroyed.<br/>
<i>Postconditions:</i> <ins>If <code>a</code> and <code>rv</code> do not refer
to the same object,</ins> <code>a</code> is equal
to the value that <code>rv</code> had before this assignment.</td>
<td>linear</td>
</tr>
<tr>
<td colspan="4" align="center">
<code>&hellip;</code>
</td>
</tr>
</table>
</blockquote>
</li>
</ol>






</body>
</html>
