|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>Issue 4283: std::trivially_relocate needs stronger preconditions on "nested" objects with dynamic lifetime</title> |
| 6 | +<meta property="og:title" content="Issue 4283: std::trivially_relocate needs stronger preconditions on "nested" objects with dynamic lifetime"> |
| 7 | +<meta property="og:description" content="C++ library issue. Status: New"> |
| 8 | +<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4283.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="4283"><a href="lwg-active.html#4283">4283</a>. <code class='backtick'>std::trivially_relocate</code> needs stronger preconditions on "nested" objects with dynamic lifetime</h3> |
| 66 | +<p><b>Section:</b> 20.2.6 <a href="https://wg21.link/obj.lifetime">[obj.lifetime]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a> |
| 67 | + <b>Submitter:</b> Giuseppe D'Angelo <b>Opened:</b> 2025-06-23 <b>Last modified:</b> 2025-06-28</p> |
| 68 | +<p><b>Priority: </b>Not Prioritized |
| 69 | +</p> |
| 70 | +<p><b>View other</b> <a href="lwg-index-open.html#obj.lifetime">active issues</a> in [obj.lifetime].</p> |
| 71 | +<p><b>View all other</b> <a href="lwg-index.html#obj.lifetime">issues</a> in [obj.lifetime].</p> |
| 72 | +<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p> |
| 73 | +<p><b>Discussion:</b></p> |
| 74 | +<p> |
| 75 | +In 20.2.6 <a href="https://wg21.link/obj.lifetime">[obj.lifetime]</a> the <code class='backtick'>std::trivially_relocate</code> function |
| 76 | +is missing a precondition, that is, that any object alive in the range being |
| 77 | +relocated is itself trivially relocatable. |
| 78 | +<p/> |
| 79 | +We know the objects in the range are trivially relocatable, because |
| 80 | +there is a <i>Mandates</i>: element for this. The current draft has precise |
| 81 | +rules to determine whether a type is trivially relocatable or not; in |
| 82 | +general, subobjects are considered there (cf. 11.2 <a href="https://wg21.link/class.prop">[class.prop]</a>, |
| 83 | +"eligible for trivial relocation", which discusses base classes and non-static |
| 84 | +data members). |
| 85 | +<p/> |
| 86 | +However these rules do not take into account objects with dynamic |
| 87 | +lifetime whose storage is being provided by (sub)objects in the range. |
| 88 | +<p/> |
| 89 | +For instance, given a <code class='backtick'>wrapper</code> type like: |
| 90 | +</p> |
| 91 | +<blockquote><pre> |
| 92 | +// wraps a T |
| 93 | +template<typename T> |
| 94 | +struct wrapper { |
| 95 | + alignas(T) std::byte data[sizeof(T)]; |
| 96 | +}; |
| 97 | +</pre></blockquote> |
| 98 | +<p> |
| 99 | +then one can build a non-trivially relocatable object into <code class='backtick'>wrapper</code> |
| 100 | +objects: |
| 101 | +</p> |
| 102 | +<blockquote><pre> |
| 103 | +struct NTR { ~NTR() {} }; |
| 104 | +static_assert(not std::is_trivially_relocatable_v<NTR>); |
| 105 | + |
| 106 | +using WS = wrapper<NTR>; |
| 107 | +static_assert(std::is_trivially_relocatable_v<WS>); // OK |
| 108 | +</pre></blockquote> |
| 109 | +<p> |
| 110 | +And now one can do this: |
| 111 | +</p> |
| 112 | +<blockquote><pre> |
| 113 | +WS* ws = /* … */; // create a wrapper |
| 114 | +new (&ws->data) NTR(); // create a NTR object into it |
| 115 | + |
| 116 | +std::trivially_relocate(ws, ws+1, dest); // should be UB |
| 117 | +</pre></blockquote> |
| 118 | +<p> |
| 119 | +Attempting to trivially relocate <code class='backtick'>*ws</code> should result in undefined |
| 120 | +behavior because <code class='backtick'>NTR</code> isn't trivially relocatable. I don't believe that |
| 121 | +this fact is correctly captured by the preconditions of |
| 122 | +<code class='backtick'>std::trivially_relocate</code>. |
| 123 | +<p/> |
| 124 | +A similar issue is present for polymorphic types. In <a href="https://wg21.link/P2786" title=" Trivial Relocatability For C++26">P2786</a>'s |
| 125 | +design polymorphic types can be trivially relocatable (assuming all the other |
| 126 | +conditions hold). Given a trivially relocatable polymorphic type <code class='backtick'>P</code>, |
| 127 | +then this code: |
| 128 | +</p> |
| 129 | +<blockquote><pre> |
| 130 | +struct P { virtual void f(); }; |
| 131 | +static_assert(std::is_trivially_relocatable_v<P>); |
| 132 | + |
| 133 | +using WP = wrapper<P>; |
| 134 | +WP* wp = /* … */; // create a wrapper |
| 135 | +new (&wp->data) P(); // create a P object into it |
| 136 | + |
| 137 | +std::trivially_relocate(wp, wp+1, dest); // implementation defined |
| 138 | +</pre></blockquote> |
| 139 | +<p> |
| 140 | +is well-defined or UB, depending on the implementation. This is because |
| 141 | +on some implementations trivially relocating a polymorphic type requires |
| 142 | +patching its virtual table pointer; cf. the discussion in chapter 15.1 |
| 143 | +of <a href="https://wg21.link/P2786R13">P2786R13</a>. However the "type erasure" done by |
| 144 | +<code>wrapper<P></code> in the example (ultimately, it is just an array |
| 145 | +of bytes) does not allow implementations to do such patching, and the code |
| 146 | +is going to fail at runtime. Therefore this case also needs to be discussed by |
| 147 | +<code class='backtick'>std::trivially_relocate</code>'s specification. |
| 148 | +</p> |
| 149 | + |
| 150 | + |
| 151 | +<p id="res-4283"><b>Proposed resolution:</b></p> |
| 152 | +<p> |
| 153 | +This wording is relative to <a href="https://wg21.link/N5008">N5008</a>. |
| 154 | +</p> |
| 155 | + |
| 156 | +<ol> |
| 157 | +<li><p>Modify 20.2.6 <a href="https://wg21.link/obj.lifetime">[obj.lifetime]</a> as indicated:</p> |
| 158 | + |
| 159 | +<blockquote class="note"> |
| 160 | +<p> |
| 161 | +[<i>Drafting note</i>: For the general part of the issue (all objects in the range must be of |
| 162 | +trivially relocatable type), we append another point at the end of the existing |
| 163 | +<i>Preconditions:</i> element of <code class='backtick'>trivially_relocate</code>.<br/> |
| 164 | +For the specifics of polymorphic types, we amend at the end of the description the existing |
| 165 | +<i>Remarks</i>: element] |
| 166 | +</p> |
| 167 | +</blockquote> |
| 168 | + |
| 169 | +<blockquote> |
| 170 | +<pre> |
| 171 | +template<class T> |
| 172 | + T* trivially_relocate(T* first, T* last, T* result); |
| 173 | +</pre> |
| 174 | +<blockquote> |
| 175 | +<p> |
| 176 | +-9- <i>Mandates</i>: […] |
| 177 | +<p/> |
| 178 | +-10- <i>Preconditions</i>: |
| 179 | +</p> |
| 180 | +<ol style="list-style-type: none"> |
| 181 | +<li><p>(10.1) — <code class='backtick'>[first, last)</code> is a valid range.</p></li> |
| 182 | +<li><p>(10.2) — <code class='backtick'>[result, result + (last - first))</code> denotes a region of storage that is a subset of the region |
| 183 | +reachable through <code class='backtick'>result</code> (6.8.4 <a href="https://wg21.link/basic.compound">[basic.compound]</a>) and suitably aligned for the type <code class='backtick'>T</code>.</p></li> |
| 184 | +<li><p>(10.3) — No element in the range <code class='backtick'>[first, last)</code> is a potentially-overlapping subobject.</p></li> |
| 185 | +<li><p><ins>(10.?) — All objects whose storage is being provided for (6.7.2 <a href="https://wg21.link/intro.object">[intro.object]</a>) by |
| 186 | +objects in the <code class='backtick'>[first, last)</code> range are of trivially relocatable type.</ins></p></li> |
| 187 | +</ol> |
| 188 | +<p> |
| 189 | +-11- <i>Postconditions</i>: […] |
| 190 | +<p/> |
| 191 | +-12- <i>Returns</i>: <code class='backtick'>result + (last - first)</code>. |
| 192 | +<p/> |
| 193 | +-13- <i>Throws</i>: Nothing. |
| 194 | +<p/> |
| 195 | +-14- <i>Complexity</i>: Linear in the length of the source range. |
| 196 | +<p/> |
| 197 | +-15- <i>Remarks</i>: The destination region of storage is considered reused (6.7.4 <a href="https://wg21.link/basic.life">[basic.life]</a>). |
| 198 | +No constructors or destructors are invoked. <ins>If any polymorphic object (11.7.3 <a href="https://wg21.link/class.virtual">[class.virtual]</a>) |
| 199 | +exists in storage provided for (6.7.2 <a href="https://wg21.link/intro.object">[intro.object]</a>) by objects in the <code class='backtick'>[first, last)</code> range, |
| 200 | +it is implementation-defined whether the behavior is undefined.</ins> |
| 201 | +<p/> |
| 202 | +[<i>Note 2</i>: Overlapping ranges are supported. — <i>end note</i>] |
| 203 | +</p> |
| 204 | +</blockquote> |
| 205 | +</blockquote> |
| 206 | +</li> |
| 207 | + |
| 208 | +</ol> |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | +</body> |
| 215 | +</html> |
0 commit comments