<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2997: LWG 491 and the specification of {forward_,}list::unique</title>
<meta property="og:title" content="Issue 2997: LWG 491 and the specification of {forward_,}list::unique">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2997.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="2997"><a href="lwg-defects.html#2997">2997</a>. LWG 491 and the specification of <code>{forward_,}list::unique</code></h3>
<p><b>Section:</b> 23.3.11.5 <a href="https://wg21.link/list.ops">[list.ops]</a>, 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> 2017-07-07 <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#list.ops">issues</a> in [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>
There are various problems with the specification of <code>list::unique</code> and its <code>forward_list</code> counterpart, some of which are obvious even on cursory inspection:
<ul>
<li>It refers to the identifiers <code>first</code> and <code>last</code>, which aren't even defined.</li>
<li>It uses <code>i - 1</code> on non-random-access iterators - in the case of <code>forward_list</code>, on forward-only iterators.</li>
<li>The resolution of LWG <a href="lwg-defects.html#202" title="unique() effects unclear when predicate not an equivalence relation (Status: CD1)">202</a><sup><a href="https://cplusplus.github.io/LWG/issue202" title="Latest snapshot">(i)</a></sup>, changing the specification of <code>std::unique</code> to require an equivalence relation
    and adjusting the order of comparison, weren't applied to these member functions.</li>
</ul>
<p/>
LWG <a href="lwg-closed.html#491" title="std::list&lt;>::unique incorrectly specified (Status: NAD)">491</a><sup><a href="https://cplusplus.github.io/LWG/issue491" title="Latest snapshot">(i)</a></sup>, which pointed out many of those problems with the specification of <code>list::unique</code>, was closed as NAD with the rationale that
<blockquote>
"All implementations known to the author of this Defect Report comply with these assumption", and "no impact on current code is expected", i.e. there is no evidence of real-world confusion or harm.
</blockquote>
That implementations somehow managed to do the right thing in spite of obviously defective standardese doesn't seem like a good reason to not fix the defects.
</p>

<p><i>[2017-07 Toronto Tuesday PM issue prioritization]</i></p>

<p>Priority 3; by the way, there's general wording in 26.2 <a href="https://wg21.link/algorithms.requirements">[algorithms.requirements]</a> p10 that lets us specify iterator arithmetic as if we were using random access iterators.</p>

<p><i>[2017-07-11 Tim comments]</i></p>

<p>I drafted the P/R fully aware of the general wording in 26.2 <a href="https://wg21.link/algorithms.requirements">[algorithms.requirements]</a> p10. However, that general wording is limited to Clause 28, so to make use of the shorthand permitted
by that wording, we would need additional wording importing it to these subclauses.
<p/>
Moreover, that general wording only defines <code>a+n</code> and <code>b-a</code>; it notably doesn't define <code>a-n</code>, which is needed here. And one cannot merely define <code>a-n</code> as
<code>a+(-n)</code> since that has undefined behavior for forward iterators.
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>
This wording is relative to <a href="https://wg21.link/n4659">N4659</a>.
</p>

<ol>
<li><p>Edit 23.3.11.5 <a href="https://wg21.link/list.ops">[list.ops]</a> as indicated:</p>
<blockquote>
<pre>
void unique();
template &lt;class BinaryPredicate&gt; void unique(BinaryPredicate binary_pred);
</pre>
<blockquote><p>
<ins>-?- <i>Requires:</i> The comparison function shall be an equivalence relation. </ins>
<p/>
-19- <i>Effects:</i> <ins>If <code>empty()</code>, has no effects. Otherwise, e</ins><del>E</del>rases all but the first element from every consecutive
group of <del>equal</del><ins>equivalent</ins> elements referred to by the iterator <code>i</code> in the range <code><del>[first + 1, last)</del><ins>[next(begin()), end())</ins></code>
for which <code><del>*i == *(i-1)</del><ins>*j == *i</ins></code> (for the version of <code>unique</code> with no arguments) or
<code><del>pred(*i, *(i - 1))</del><ins>pred(*j, *i)</ins></code> (for the version of <code>unique</code> with a predicate argument) holds<ins>,
where <code>j</code> is an iterator in <code>[begin(), end())</code> such that <code>next(j) == i</code></ins>. Invalidates only the iterators and references to the erased elements.
<p/>
-20- <i>Throws:</i> Nothing unless an exception is thrown by <del><code>*i == *(i-1)</code> or <code>pred(*i, *(i - 1))</code></del><ins> the equality comparison or the predicate</ins>.
<p/>
-21- <i>Complexity:</i> If <del>the range <code>[first, last)</code> is not empty</del><ins><code>!empty()</code></ins>, exactly <code><del>(last - first) - 1</del><ins>size() - 1</ins></code>
applications of the corresponding predicate, otherwise no applications of the predicate.
</p>
</blockquote>
</blockquote>
</li>

<li><p>Edit  [forwardlist.ops] as indicated:</p>
<blockquote>
<pre>
void unique();
template &lt;class BinaryPredicate&gt; void unique(BinaryPredicate binary_pred);
</pre>
<blockquote><p>
<ins>-?- <i>Requires:</i> The comparison function shall be an equivalence relation. </ins>
<p/>
-16- <i>Effects:</i> <ins>If <code>empty()</code>, has no effects. Otherwise, e</ins><del>E</del>rases all but the first element from every consecutive
group of <del>equal</del><ins>equivalent</ins> elements referred to by the iterator <code>i</code> in the range <code><del>[first + 1, last)</del><ins>[next(begin()), end())</ins></code>
for which <code><del>*i == *(i-1)</del><ins>*j == *i</ins></code> (for the version with no arguments) or
<code><del>pred(*i, *(i - 1))</del><ins>pred(*j, *i)</ins></code> (for the version with a predicate argument) holds<ins>,
where <code>j</code> is an iterator in <code>[begin(), end())</code> such that <code>next(j) == i</code></ins>. Invalidates only the iterators and references to the erased elements.
<p/>
-17- <i>Throws:</i> Nothing unless an exception is thrown by the equality comparison or the predicate.
<p/>
-18- <i>Complexity:</i> If <del>the range <code>[first, last)</code> is not empty</del><ins><code>!empty()</code></ins>, exactly
<code><del>(last - first) - 1</del><ins>distance(begin(), end()) - 1</ins></code> applications of the corresponding predicate,
    otherwise no applications of the predicate.
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>
<p><i>[2021-02-21 Tim redrafts]</i></p>

<p>The wording below incorporates
<a href="https://github.com/cplusplus/draft/pull/4465">editorial pull request 4465</a>. </p>

<p><i>[2021-05-21; Reflector poll]</i></p>

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

<p><i>[2021-06-07 Approved at June 2021 virtual plenary. Status changed: Voting &rarr; WP.]</i></p>



<p id="res-2997"><b>Proposed resolution:</b></p>

<p>
This wording is relative to <a href="https://wg21.link/n4878">N4878</a>.
</p>

<ol>
<li><p>Edit 23.3.11.5 <a href="https://wg21.link/list.ops">[list.ops]</a> as indicated:</p>
<blockquote><p>
-1- Since lists allow fast insertion and erasing from the middle of a list,
certain operations are provided specifically for them.<sup>222</sup>
In this subclause, arguments for a template parameter named <code>Predicate</code> or
<code>BinaryPredicate</code> shall meet the corresponding requirements in
26.2 <a href="https://wg21.link/algorithms.requirements">[algorithms.requirements]</a>. <ins>The semantics of <code>i + n</code>
and <code>i - n</code>, where <code>i</code> is an iterator into the list and <code>n</code>
is an integer, are the same as those of <code>next(i, n)</code> and <code>prev(i, n)</code>,
respectively.</ins>
For <code>merge</code> and <code>sort</code>, the definitions and requirements in
26.8 <a href="https://wg21.link/alg.sorting">[alg.sorting]</a> apply.
<p/>
[&hellip;]
</p>
<pre>
void unique();
template&lt;class BinaryPredicate&gt; void unique(BinaryPredicate binary_pred);
</pre>
<blockquote><p>
<ins>-?- Let <code>binary_pred</code> be <code>equal_to&lt;&gt;{}</code> for the first overload.</ins>
<p/>
<ins>-?- <i>Preconditions:</i> <code>binary_pred</code> is an equivalence relation.</ins>
<p/>
-20- <i>Effects:</i> Erases all but the first element from every consecutive
group of <del>equal</del><ins>equivalent</ins> elements<ins>. That is, for a nonempty list, erases all elements</ins>
referred to by the iterator <code>i</code> in the range <code><del>[first + 1, last)</del><ins>[begin() + 1, end())</ins></code>
for which <del><code>*i == *(i-1)</code> (for the version of <code>unique</code> with no arguments) or</del>
<code><ins>binary_</ins>pred(*i, *(i - 1))</code> <ins>is <code>true</code></ins><del>(for the version of <code>unique</code> with a predicate argument) holds</del>.
Invalidates only the iterators and references to the erased elements.
<p/>
-21- <i>Returns</i>: The number of elements erased.
<p/>
-22- <i>Throws:</i> Nothing unless an exception is thrown by <del><code>*i == *(i-1)</code> or <code>pred(*i, *(i - 1))</code></del><ins>the predicate</ins>.
<p/>
-23- <i>Complexity:</i> If <del>the range <code>[first, last)</code> is not empty</del><ins><code>empty()</code> is <code>false</code></ins>, exactly <code><del>(last - first) - 1</del><ins>size() - 1</ins></code>
applications of the corresponding predicate, otherwise no applications of the predicate.
</p>
</blockquote>
</blockquote>
</li>

<li><p>Edit  [forwardlist.ops] as indicated:</p>
<blockquote>
<p>
-1- In this subclause, arguments for a template parameter named <code>Predicate</code> or
<code>BinaryPredicate</code> shall meet the corresponding requirements in
26.2 <a href="https://wg21.link/algorithms.requirements">[algorithms.requirements]</a>. <ins>The semantics of <code>i + n</code>,
where <code>i</code> is an iterator into the list and <code>n</code>
is an integer, are the same as those of <code>next(i, n)</code>. The expression
<code>i - n</code>, where <code>i</code> is an iterator into the list and <code>n</code>
is an integer, means an iterator <code>j</code> such that <code>j + n == i</code> is
<code>true</code>.</ins>
For <code>merge</code> and <code>sort</code>, the definitions and requirements in
26.8 <a href="https://wg21.link/alg.sorting">[alg.sorting]</a> apply.
<p/>
[&hellip;]
</p>
<pre>
void unique();
template&lt;class BinaryPredicate&gt; void unique(BinaryPredicate binary_pred);
</pre>
<blockquote><p>
<ins>-?- Let <code>binary_pred</code> be <code>equal_to&lt;&gt;{}</code> for the first overload.</ins>
<p/>
<ins>-?- <i>Preconditions:</i> <code>binary_pred</code> is an equivalence relation.</ins>
<p/>
-18- <i>Effects:</i> Erases all but the first element from every consecutive
group of <del>equal</del><ins>equivalent</ins> elements<ins>. That is, for a nonempty list, 
erases all elements</ins> referred to by the iterator <code>i</code> in the range 
<code><del>[first + 1, last)</del><ins>[begin() + 1, end())</ins></code>
for which <del><code>*i == *(i-1)</code> (for the version with no arguments) or</del>
<code><ins>binary_</ins>pred(*i, *(i - 1))</code> <ins>is <code>true</code></ins> <del>(for the 
version with a predicate argument) holds</del>. Invalidates only the iterators and references 
to the erased elements.
<p/>
-19- <i>Returns</i>: The number of elements erased.
<p/>
-20- <i>Throws:</i> Nothing unless an exception is thrown by <del>the equality comparison or</del> 
the predicate.
<p/>
-21- <i>Complexity:</i> If <del>the range <code>[first, last)</code> is not empty</del><ins><code>empty()</code> 
is <code>false</code></ins>, exactly <code><del>(last - first) - 1</del><ins>distance(begin(), end()) - 1</ins></code> 
applications of the corresponding predicate, otherwise no applications of the predicate.
</p>
</blockquote>
</blockquote>
</li>
</ol>




</body>
</html>
