<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3088: forward_list::merge behavior unclear when passed *this</title>
<meta property="og:title" content="Issue 3088: forward_list::merge behavior unclear when passed *this">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3088.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="3088"><a href="lwg-defects.html#3088">3088</a>. <code>forward_list::merge</code> behavior unclear when passed <code>*this</code></h3>
<p><b>Section:</b> 23.3.7.6 <a href="https://wg21.link/forward.list.ops">[forward.list.ops]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Tim Song <b>Opened:</b> 2018-03-19 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#forward.list.ops">issues</a> in [forward.list.ops].</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#300" title="list::merge() specification incomplete (Status: CD1)">300</a><sup><a href="https://cplusplus.github.io/LWG/issue300" title="Latest snapshot">(i)</a></sup> changed <code>list::merge</code> to be a no-op when passed <code>*this</code>, but there's
no equivalent rule for <code>forward_list::merge</code>.
Presumably the <code>forward_list</code> proposal predated the adoption of LWG 300's PR and was never updated
for the change. Everything in the discussion of that issue applies mutatis mutandis to the current
specification of <code>forward_list::merge</code>.
</p>

<p><i>[2018-06-18 after reflector discussion]</i></p>

<p>Priority set to 3</p>

<p><i>[2019-07-30 Tim provides updated PR]</i></p>

<p>Per the comments during issue prioritization, the new PR tries to synchronize the wording
between <code>list::merge</code> and <code>forward_list::merge</code>.</p>
<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4727">N4727</a>.</p>

<ol>
<li><p>Edit  [forwardlist.ops] as indicated:</p>
<blockquote>
<pre>
void merge(forward_list&amp; x);
void merge(forward_list&amp;&amp; x);
template&lt;class Compare&gt; void merge(forward_list&amp; x, Compare comp);
template&lt;class Compare&gt; void merge(forward_list&amp;&amp; x, Compare comp);
</pre>
<blockquote>
<p>
-20- <i>Requires:</i> <code>*this</code> and <code>x</code> are both sorted with respect to the comparator
<code>operator&lt;</code> (for the first two overloads) or <code>comp</code> (for the last two overloads),
and <code>get_allocator() == x.get_allocator()</code> is <code>true</code>.
<p/>
-21- <i>Effects:</i> <ins>If <code>addressof(x) == this</code>, does nothing. Otherwise,
m</ins><del>M</del>erges the two sorted ranges <code>[begin(), end())</code> and <code>[x.begin(), x.end())</code>.
<ins>The result is a range that is sorted with respect to the comparator <code>operator&lt;</code>
(for the first two overloads) or <code>comp</code> (for the last two overloads).</ins> <code>x</code> is
empty after the merge. If an exception is thrown other than by a comparison there are no effects.
Pointers and references to the moved elements of <code>x</code> now refer to those same elements but
as members of <code>*this</code>. Iterators referring to the moved elements will continue to refer to
their elements, but they now behave as iterators into <code>*this</code>, not into <code>x</code>.
<p/>
-22- <i>Remarks:</i> Stable (16.4.6.8 <a href="https://wg21.link/algorithm.stable">[algorithm.stable]</a>). The behavior is undefined if
<code>get_allocator() != x.get_allocator()</code>.
<p/>
-23- <i>Complexity:</i> At most <code>distance(begin(), end()) + distance(x.begin(), x.end()) - 1</code>
comparisons <ins>if <code>addressof(x) != this</code>; otherwise, no comparisons are performed</ins>.
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>
<p><i>[2021-05-22 Tim syncs wording to the current working draft]</i></p>


<p><i>[2022-01-31; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after six votes in favour during reflector poll.
</p>

<p><i>[2022-02-10 Approved at February 2022 virtual plenary. Status changed: Tentatively Ready &rarr; WP.]</i></p>



<p id="res-3088"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/N4901" title=" Working Draft, Standard for Programming Language C++">N4901</a>.</p>

<ol>
<li><p>Edit 23.3.7.6 <a href="https://wg21.link/forward.list.ops">[forward.list.ops]</a> as indicated:</p>
<blockquote>
<pre>
void merge(forward_list&amp; x);
void merge(forward_list&amp;&amp; x);
template&lt;class Compare&gt; void merge(forward_list&amp; x, Compare comp);
template&lt;class Compare&gt; void merge(forward_list&amp;&amp; x, Compare comp);
</pre>
<blockquote>
<p>
<ins>-?- Let <code>comp</code> be <code>less&lt;&gt;{}</code> for the first two overloads.</ins>
<p/>
-24- <i>Preconditions:</i> <code>*this</code> and <code>x</code> are both sorted with respect to the comparator
<del><code>operator&lt;</code> (for the first two overloads) or</del> <code>comp</code>
<del> (for the last two overloads)</del>,
and <code>get_allocator() == x.get_allocator()</code> is <code>true</code>.
<p/>
-25- <i>Effects:</i> <ins>If <code>addressof(x) == this</code>, there are no effects. Otherwise,
m</ins><del>M</del>erges the two sorted ranges <code>[begin(), end())</code> and <code>[x.begin(), x.end())</code>.
<ins>The result is a range that is sorted with respect to the comparator <code>comp</code>.</ins> <del><code>x</code> is
empty after the merge. If an exception is thrown other than by a comparison there are no effects.</del>
Pointers and references to the moved elements of <code>x</code> now refer to those same elements but
as members of <code>*this</code>. Iterators referring to the moved elements will continue to refer to
their elements, but they now behave as iterators into <code>*this</code>, not into <code>x</code>.
<p/>
-26- <i>Complexity:</i> At most <code>distance(begin(), end()) + distance(x.begin(), x.end()) - 1</code>
comparisons <ins>if <code>addressof(x) != this</code>; otherwise, no comparisons are performed</ins>.
<p/>
-27- <i>Remarks:</i> Stable (16.4.6.8 <a href="https://wg21.link/algorithm.stable">[algorithm.stable]</a>). <ins>If <code>addressof(x) != this</code>,
<code>x</code> is empty after the merge. No elements are copied by this operation.
If an exception is thrown other than by a comparison there are no effects.</ins>
</p>
</blockquote>
</blockquote>
</li>
<li><p>Edit 23.3.11.5 <a href="https://wg21.link/list.ops">[list.ops]</a> as indicated:</p>
<blockquote>
<pre>
void merge(list&amp; x);
void merge(list&amp;&amp; x);
template&lt;class Compare&gt; void merge(list&amp; x, Compare comp);
template&lt;class Compare&gt; void merge(list&amp;&amp; x, Compare comp);
</pre>
<blockquote>
<p>
<ins>-?- Let <code>comp</code> be <code>less&lt;&gt;{}</code> for the first two overloads.</ins>
<p/>
-26- <i>Preconditions:</i> <del>Both the list and the argument list shall be</del><ins><code>*this</code> and <code>x</code> are both</ins>
sorted with respect to the comparator <del><code>operator&lt;</code>
(for the first two overloads) or </del><code>comp</code><del> (for the last two overloads)</del>, and
<code>get_allocator() == x.get_allocator()</code> is <code>true</code>.
<p/>
-27- <i>Effects:</i> If <code>addressof(x) == this</code>, <del>does nothing; o</del><ins>there are no effects. O</ins>therwise,
merges the two sorted ranges <code>[begin(), end())</code> and <code>[x.begin(), x.end())</code>.
The result is a range <del>in which the elements will be sorted in non-decreasing order according to the ordering
defined by <code>comp</code>; that is, for every iterator <code>i</code>, in the range other than the first, the condition
<code>comp(*i, *(i - 1))</code> will be <code>false</code></del><ins>that is sorted with respect to the comparator
<code>comp</code></ins>. Pointers and references to the moved elements of <code>x</code> now refer to those same elements
but as members of <code>*this</code>. Iterators referring to the moved elements will continue to refer to their elements,
but they now behave as iterators into <code>*this</code>, not into <code>x</code>.
<p/>
-28- <i>Complexity:</i> At most <code>size() + x.size() - 1</code> <del>applications of <code>comp</code></del><ins>comparisons</ins>
if <code>addressof(x) != this</code>; otherwise, no <del>applications of <code>comp</code></del><ins>comparisons</ins> are performed.
<del>If an exception is thrown other than by a comparison there are no effects.</del><p/>
-29- <i>Remarks:</i> Stable (16.4.6.8 <a href="https://wg21.link/algorithm.stable">[algorithm.stable]</a>). If <code>addressof(x) != this</code>, <del>the range
<code>[x.begin(), x.end())</code></del><ins><code>x</code></ins> is empty after the merge. No elements are copied by this operation.
<ins>If an exception is thrown other than by a comparison there are no effects.</ins>
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
