<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4151: Precondition of inplace_vector::swap</title>
<meta property="og:title" content="Issue 4151: Precondition of inplace_vector::swap">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4151.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="4151"><a href="lwg-active.html#4151">4151</a>. Precondition of <code>inplace_vector::swap</code></h3>
<p><b>Section:</b> 23.3.16.5 <a href="https://wg21.link/inplace.vector.modifiers">[inplace.vector.modifiers]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Arthur O'Dwyer <b>Opened:</b> 2024-09-07 <b>Last modified:</b> 2024-09-18</p>
<p><b>Priority: </b>2
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Right now <code>inplace_vector::swap</code> has only a declaration in the class synopsis; it doesn't
specify what the behavior of <code>swap</code> for <code>inplace_vector</code>s actually is. So I think
the behavior ends up being governed by 23.2.2.2 <a href="https://wg21.link/container.reqmts">[container.reqmts]</a>, which are written from
the point of view of a container that manages an external heap allocation so that swapping
containers doesn't touch the elements at all, just changes their ownership.
<p/>
<code>inplace_vector::swap</code> actually works more like <code>array::swap</code>, which has its own
specification in 23.3.3.3 <a href="https://wg21.link/array.members">[array.members]</a>, where it is defined in terms of <code>std::swap_ranges</code>.
The <code>std::swap_ranges</code> algorithm (26.7.3 <a href="https://wg21.link/alg.swap">[alg.swap]</a>) has a precondition! This
precondition is missing from <code>inplace_vector::swap</code>.
<p/>
That is, I think we currently have no wording that explains why
</p>
<blockquote><pre>
std::pmr::monotonic_buffer_resource mr1;
std::inplace_vector&lt;std::pmr::vector&lt;int&gt;, 2&gt; v1, v2;
v1.emplace_back(1, &amp;mr1);
v2.emplace_back(1);
v1.swap(v2);
</pre></blockquote>
<p>
is undefined behavior. The current spec seems to say this Just Works, even though it physically
cannot work because <code>v1[0]</code> and <code>v2[0]</code> don't dynamically meet the semantic requirements
of being "swappable with" each other, and <code>v1.swap(v2)</code> is necessarily going to try to swap them.
</p>

<p><i>[2024-09-18; Reflector poll]</i></p>

<p>
Set priority to 2 after reflector poll.
The <i>Preconditions</i>: (but not the <i>Effects</i>:) would be changed
again if <a href="https://wg21.link/P3160R2" title=" An allocator-aware `inplace_vector`">P3160R2</a> is approved.
</p>



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

<ol>
<li><p>Modify 23.3.16.5 <a href="https://wg21.link/inplace.vector.modifiers">[inplace.vector.modifiers]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note</i>: It is suggested to add the new wording to the end of the existing
subclause]
</p>
</blockquote>

<blockquote><pre>
<ins>constexpr void swap(inplace_vector&amp; x) noexcept(N == 0 ||
  (is_nothrow_swappable_v&lt;T&gt; &amp;&amp; is_nothrow_move_constructible_v&lt;T&gt;));</ins>
</pre>
<blockquote>
<p>
<ins>-?- <i>Preconditions</i>: Let <code><i>M</i></code> be <code>min(size(), x.size())</code>. For each
non-negative integer <code><i>n</i> &lt; <i>M</i></code>, <code>(*this)[<i>n</i>]</code> is swappable with
(16.4.4.3 <a href="https://wg21.link/swappable.requirements">[swappable.requirements]</a>) <code>x[<i>n</i>]</code>.</ins>
<p/>
<ins>-?- <i>Effects</i>: Exchanges the contents of <code>*this</code> and <code>x</code>.</ins>
</p>
</blockquote>
</blockquote>
</li>

</ol>






</body>
</html>
