|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>Issue 4251: Move assignment for indirect unnecessarily requires copy construction</title> |
| 6 | +<meta property="og:title" content="Issue 4251: Move assignment for indirect unnecessarily requires copy construction"> |
| 7 | +<meta property="og:description" content="C++ library issue. Status: New"> |
| 8 | +<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4251.html"> |
| 9 | +<meta property="og:type" content="website"> |
| 10 | +<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png"> |
| 11 | +<meta property="og:image:alt" content="C++ logo"> |
| 12 | +<style> |
| 13 | + p {text-align:justify} |
| 14 | + li {text-align:justify} |
| 15 | + pre code.backtick::before { content: "`" } |
| 16 | + pre code.backtick::after { content: "`" } |
| 17 | + blockquote.note |
| 18 | + { |
| 19 | + background-color:#E0E0E0; |
| 20 | + padding-left: 15px; |
| 21 | + padding-right: 15px; |
| 22 | + padding-top: 1px; |
| 23 | + padding-bottom: 1px; |
| 24 | + } |
| 25 | + ins {background-color:#A0FFA0} |
| 26 | + del {background-color:#FFA0A0} |
| 27 | + table.issues-index { border: 1px solid; border-collapse: collapse; } |
| 28 | + table.issues-index th { text-align: center; padding: 4px; border: 1px solid; } |
| 29 | + table.issues-index td { padding: 4px; border: 1px solid; } |
| 30 | + table.issues-index td:nth-child(1) { text-align: right; } |
| 31 | + table.issues-index td:nth-child(2) { text-align: left; } |
| 32 | + table.issues-index td:nth-child(3) { text-align: left; } |
| 33 | + table.issues-index td:nth-child(4) { text-align: left; } |
| 34 | + table.issues-index td:nth-child(5) { text-align: center; } |
| 35 | + table.issues-index td:nth-child(6) { text-align: center; } |
| 36 | + table.issues-index td:nth-child(7) { text-align: left; } |
| 37 | + table.issues-index td:nth-child(5) span.no-pr { color: red; } |
| 38 | + @media (prefers-color-scheme: dark) { |
| 39 | + html { |
| 40 | + color: #ddd; |
| 41 | + background-color: black; |
| 42 | + } |
| 43 | + ins { |
| 44 | + background-color: #225522 |
| 45 | + } |
| 46 | + del { |
| 47 | + background-color: #662222 |
| 48 | + } |
| 49 | + a { |
| 50 | + color: #6af |
| 51 | + } |
| 52 | + a:visited { |
| 53 | + color: #6af |
| 54 | + } |
| 55 | + blockquote.note |
| 56 | + { |
| 57 | + background-color: rgba(255, 255, 255, .10) |
| 58 | + } |
| 59 | + } |
| 60 | +</style> |
| 61 | +</head> |
| 62 | +<body> |
| 63 | +<hr> |
| 64 | +<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> |
| 65 | +<h3 id="4251"><a href="lwg-active.html#4251">4251</a>. Move assignment for <code class='backtick'>indirect</code> unnecessarily requires copy construction</h3> |
| 66 | +<p><b>Section:</b> 20.4.1.5 <a href="https://wg21.link/indirect.asgn">[indirect.asgn]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a> |
| 67 | + <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2025-05-01 <b>Last modified:</b> 2025-05-01</p> |
| 68 | +<p><b>Priority: </b>Not Prioritized |
| 69 | +</p> |
| 70 | +<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p> |
| 71 | +<p><b>Discussion:</b></p> |
| 72 | +<p> |
| 73 | +The move assignment operator for <code class='backtick'>indirect</code> says: |
| 74 | +<blockquote> |
| 75 | +<i>Mandates</i>: <code>is_copy_constructible_t<T></code> is <code class='backtick'>true</code>. |
| 76 | +</blockquote> |
| 77 | + |
| 78 | +However, the only way it ever construct an object is: |
| 79 | +<blockquote> |
| 80 | +constructs a new owned object with the owned object of <code class='backtick'>other</code> as the argument |
| 81 | +as an rvalue |
| 82 | +</blockquote> |
| 83 | +and that only ever happens when <code><i>alloc</i> == other.<i>alloc</i></code> |
| 84 | +is false. |
| 85 | +</p> |
| 86 | +<p> |
| 87 | +It seems like we should require <code class='backtick'>is_move_constructible_v</code> instead, |
| 88 | +and only if the allocator traits mean we need to construct an object. |
| 89 | +</p> |
| 90 | +<p> |
| 91 | +Additionally, the noexcept-specifier for the move assignment doesn't match |
| 92 | +the effects. The noexcept-specifier says it can't throw if POCMA is true, |
| 93 | +but nothing in the effects says that ownership can be transferred in that case. |
| 94 | +We only do a non-throwing transfer when the allocators are equal. |
| 95 | +I think we should also transfer ownership when |
| 96 | +</p> |
| 97 | + |
| 98 | + |
| 99 | +<p id="res-4251"><b>Proposed resolution:</b></p> |
| 100 | +<p> |
| 101 | +This wording is relative to <a href="https://wg21.link/N5008">N5008</a>. |
| 102 | +</p> |
| 103 | +<ol> |
| 104 | + |
| 105 | +<li><p>Modify 20.4.1.5 <a href="https://wg21.link/indirect.asgn">[indirect.asgn]</a> as indicated:</p> |
| 106 | +<blockquote> |
| 107 | +<pre><code> |
| 108 | +constexpr indirect& operator=(indirect&& other) |
| 109 | + noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value || |
| 110 | + allocator_traits<Allocator>::is_always_equal::value); |
| 111 | +</code></pre> |
| 112 | +<blockquote> |
| 113 | +<p> |
| 114 | +-5- <i>Mandates</i>: |
| 115 | +<ins> |
| 116 | +If |
| 117 | +<code>allocator_traits<Allocator>::propagate_on_container_move_assignment::value</code> |
| 118 | +is <code class='backtick'>false</code> |
| 119 | +and |
| 120 | +<code>allocator_traits<Allocator>::is_always_equal::value</code> |
| 121 | +is <code class='backtick'>false</code>, |
| 122 | +</ins> |
| 123 | +<code>is_<del>copy</del><ins>move</ins>_constructible_t<T></code> is <code class='backtick'>true</code>. |
| 124 | +</p> |
| 125 | +<p> |
| 126 | +-6- <i>Effects</i>: |
| 127 | +If <code class='backtick'>addressof(other) == this</code> is <code class='backtick'>true</code>, there are no effects. |
| 128 | +Otherwise: |
| 129 | +<ol style="list-style-type:none"> |
| 130 | +<li>(6.1) — |
| 131 | +The allocator needs updating if |
| 132 | +<code>allocator_traits<Allocator>::propagate_on_container_move_assignment::value</code> |
| 133 | +is <code class='backtick'>true</code>. |
| 134 | +</li> |
| 135 | +<li>(6.2) — |
| 136 | +If <code class='backtick'>other</code> is valueless, <code class='backtick'>*this</code> becomes valueless<del> and the owned object |
| 137 | +in <code class='backtick'>*this</code>, if any, is destroyed using |
| 138 | +<code>allocator_traits<Allocator>::destroy</code> |
| 139 | +and then the storage is deallocated</del>. |
| 140 | +</li> |
| 141 | +<li>(6.3) — |
| 142 | +Otherwise, |
| 143 | +<ins>if the allocator needs updating or</ins> |
| 144 | +if <code><i>alloc</i> == other.<i>alloc</i></code> is <code class='backtick'>true</code>, |
| 145 | +<del> |
| 146 | +swaps the owned objects in <code class='backtick'>*this</code> and <code class='backtick'>other</code>; |
| 147 | +the owned object in <code class='backtick'>other</code>, if any, is then destroyed using |
| 148 | +<code>allocator_traits<Allocator>::destroy</code> |
| 149 | +and then the storage is deallocated |
| 150 | +</del> |
| 151 | +<ins><code class='backtick'>*this</code> takes ownership of the owned object of <code class='backtick'>other</code></ins>. |
| 152 | +</li> |
| 153 | +<li>(6.4) — |
| 154 | +Otherwise, constructs a new owned object with the owned object of <code class='backtick'>other</code> |
| 155 | +as the argument as an rvalue, using either the allocator in <code class='backtick'>*this</code> |
| 156 | +or the allocator in <code class='backtick'>other</code> if the allocator needs updating. |
| 157 | +</li> |
| 158 | +<li>(6.5) — |
| 159 | +The previously owned object in <code class='backtick'>*this</code>, if any, is destroyed using |
| 160 | +<code>allocator_traits<Allocator>::destroy</code> |
| 161 | +and then the storage is deallocated. |
| 162 | +</li> |
| 163 | +<li>(6.6) — |
| 164 | +If the allocator needs updating, |
| 165 | +the allocator in <code class='backtick'>*this</code> is replaced with a copy of the allocator in <code class='backtick'>other</code>. |
| 166 | +</li> |
| 167 | +</ol> |
| 168 | +</p> |
| 169 | +<p>-7- <i>Postcondition</i>: <code class='backtick'>other</code> is valueless.</p> |
| 170 | +</blockquote> |
| 171 | +</blockquote> |
| 172 | +</li> |
| 173 | +</ol> |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +</body> |
| 180 | +</html> |
0 commit comments