<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4251: Move assignment for indirect unnecessarily requires copy construction</title>
<meta property="og:title" content="Issue 4251: Move assignment for indirect unnecessarily requires copy construction">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4251.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#New">New</a> status.</em></p>
<h3 id="4251"><a href="lwg-active.html#4251">4251</a>. Move assignment for <code class='backtick'>indirect</code> unnecessarily requires copy construction</h3>
<p><b>Section:</b> 99 [indirect.asgn] <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2025-05-01 <b>Last modified:</b> 2025-06-12</p>
<p><b>Priority: </b>1
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The move assignment operator for <code class='backtick'>indirect</code> says:
<blockquote>
<i>Mandates</i>: <code>is_copy_constructible_t&lt;T&gt;</code> is <code class='backtick'>true</code>.
</blockquote>

However, the only way it ever construct an object is:
<blockquote>
constructs a new owned object with the owned object of <code class='backtick'>other</code> as the argument
as an rvalue
</blockquote>
and that only ever happens when <code><i>alloc</i> == other.<i>alloc</i></code>
is false.
</p>
<p>
It seems like we should require <code class='backtick'>is_move_constructible_v</code> instead,
and only if the allocator traits mean we need to construct an object.
(Technically move-constructible might not be correct, because the allocator's
<code class='backtick'>construct</code> member might use a different constructor).
</p>
<p>
Additionally, the noexcept-specifier for the move assignment doesn't match
the effects. The noexcept-specifier says it can't throw if POCMA is true,
but nothing in the effects says that ownership can be transferred in that case;
we only do a non-throwing transfer when the allocators are equal.
I think we <i>should</i> transfer ownership when POCMA is true,
which would make the noexcept-specifier correct.
</p>

<p><i>[2025-06-12; Reflector poll]</i></p>

<p>
Set priority to 1 after reflector poll.
</p>
<p>
Similar change needed for <code class='backtick'>std::polymorphic</code>.
</p>



<p id="res-4251"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N5008" title=" Working Draft, Programming Languages — C++">N5008</a>.
</p>
<ol>

<li><p>Modify 99 [indirect.asgn] as indicated:</p>
<blockquote>
<pre><code>
constexpr indirect&amp; operator=(indirect&amp;&amp; other)
  noexcept(allocator_traits&lt;Allocator&gt;::propagate_on_container_move_assignment::value ||
           allocator_traits&lt;Allocator&gt;::is_always_equal::value);
</code></pre>
<blockquote>
<p>
-5- <i>Mandates</i>:
<ins>
If
<code>allocator_traits&lt;Allocator&gt;::propagate_on_container_move_assignment::value</code>
is <code class='backtick'>false</code>
and
<code>allocator_traits&lt;Allocator&gt;::is_always_equal::value</code>
is <code class='backtick'>false</code>,
</ins>
<code>is_<del>copy</del><ins>move</ins>_constructible_t&lt;T&gt;</code> is <code class='backtick'>true</code>.
</p>
<p>
-6- <i>Effects</i>:
If <code class='backtick'>addressof(other) == this</code> is <code class='backtick'>true</code>, there are no effects.
Otherwise:
<ol style="list-style-type:none">
<li>(6.1) &mdash;
The allocator needs updating if
<code>allocator_traits&lt;Allocator&gt;::propagate_on_container_move_assignment::value</code>
is <code class='backtick'>true</code>.
</li>
<li>(6.2) &mdash;
If <code class='backtick'>other</code> is valueless, <code class='backtick'>*this</code> becomes valueless<del> and the owned object
in <code class='backtick'>*this</code>, if any, is destroyed using
<code>allocator_traits&lt;Allocator&gt;::destroy</code>
and then the storage is deallocated</del>.
</li>
<li>(6.3) &mdash;
Otherwise,
<ins>if the allocator needs updating or</ins>
if <code><i>alloc</i> == other.<i>alloc</i></code> is <code class='backtick'>true</code>,
<del>
swaps the owned objects in <code class='backtick'>*this</code> and <code class='backtick'>other</code>;
the owned object in <code class='backtick'>other</code>, if any, is then destroyed using
<code>allocator_traits&lt;Allocator&gt;::destroy</code>
and then the storage is deallocated
</del>
<ins><code class='backtick'>*this</code> takes ownership of the owned object of <code class='backtick'>other</code></ins>.
</li>
<li>(6.4) &mdash;
Otherwise, constructs a new owned object with the owned object of <code class='backtick'>other</code>
as the argument as an rvalue, using either the allocator in <code class='backtick'>*this</code>
or the allocator in <code class='backtick'>other</code> if the allocator needs updating.
</li>
<li>(6.5) &mdash;
The previously owned object in <code class='backtick'>*this</code>, if any, is destroyed using
<code>allocator_traits&lt;Allocator&gt;::destroy</code>
and then the storage is deallocated.
</li>
<li>(6.6) &mdash;
If the allocator needs updating,
the allocator in <code class='backtick'>*this</code> is replaced with a copy of the allocator in <code class='backtick'>other</code>.
</li>
</ol>
</p>
<p>-7- <i>Postcondition</i>: <code class='backtick'>other</code> is valueless.</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
