<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1207: Underspecified std::list operations?</title>
<meta property="og:title" content="Issue 1207: Underspecified std::list operations?">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1207.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++11">C++11</a> status.</em></p>
<h3 id="1207"><a href="lwg-defects.html#1207">1207</a>. Underspecified <code>std::list</code> operations?</h3>
<p><b>Section:</b> 23.3.11.5 <a href="https://wg21.link/list.ops">[list.ops]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Lo&iuml;c Joly <b>Opened:</b> 2009-09-13 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</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++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
It looks to me like some operations of <code>std::list</code>
(<code>sort</code>, <code>reverse</code>, <code>remove</code>, <code>unique</code> &amp;
<code>merge</code>) do not specify the validity of iterators, pointers &amp;
references to elements of the list after those operations. Is it implied
by some other text in the standard?
</p>

<p>
I believe <code>sort</code> &amp; <code>reverse</code> do not invalidating
anything, <code>remove</code> &amp; <code>unique</code> only invalidates what
refers to erased elements, <code>merge</code> does not invalidate anything
(with the same precision as <code>splice</code> for elements who changed of
container). Are those assumptions correct ?
</p>

<p><i>[
2009-12-08 Jonathan Wakely adds:
]</i></p>


<blockquote>
<p>
23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a> paragraph 11 says iterators
aren't invalidated unless specified, so I don't think it needs to be repeated on
every function that doesn't invalidate iterators. <code>list::unique</code> says it
"eliminates" elements, that should probably be "erases" because IMHO that term
is used elsewhere and so makes it clearer that iterators to the erased elements
are invalidated.
</p>

<p>
<code>list::merge</code> coud use the same wording as <code>list::splice</code> w.r.t
iterators and references to moved elements.
</p>

<p>
Suggested resolution:
</p>

<p>
In 23.3.11.5 <a href="https://wg21.link/list.ops">[list.ops]</a> change paragraph 19
</p>

<blockquote><pre>
                                 void unique();
template &lt;class BinaryPredicate&gt; void unique(BinaryPredicate binary_pred);
</pre>
<blockquote><p>
<i>Effects:</i> <del>Eliminates</del> <ins>Erases</ins> all but the first
element from every consecutive group ...
</p></blockquote>
</blockquote>

<p>
Add to the end of paragraph 23
</p>

<blockquote><pre>
void                          merge(list&lt;T,Allocator&gt;&amp;&amp; x);
template &lt;class Compare&gt; void merge(list&lt;T,Allocator&gt;&amp;&amp; x, Compare comp);
</pre>
<blockquote>
<p>...</p>
<p>
<i>Effects:</i> ... 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 false.
<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>.</ins>
</p>
</blockquote>
</blockquote>
</blockquote>

<p><i>[
2009-12-12 Lo&iuml;c adds wording.
]</i></p>


<p><i>[
2010-02-10 Moved to Tentatively Ready after 5 positive votes on c++std-lib.
]</i></p>


<p><i>[
2010-02-10 Alisdair opens:
]</i></p>


<blockquote>
<p>
I object to the current resolution of #1207.  I believe it is overly strict with
regard to <code>list</code> end iterators, being the only mutating operations to
require such stability.
</p>

<p>
More importantly, the same edits need to be applied to <code>forward_list</code>,
which uses slightly different words to describe some of these operations so may
require subtly different edits (not checked.)
</p>

<p>
I am prepared to pick up the <code>end()</code> iterator as a separate (new) issue,
as part of the FCD ballot review (BSI might tell me 'no' first ;~) but I do want
to see <code>forward_list</code> adjusted at the same time.
</p>
</blockquote>

<p><i>[
2010-03-28 Daniel adds the first 5 bullets in an attempt to address Alisdair's
concerns.
]</i></p>



<p><i>[
2010 Rapperswil:
]</i></p>


<blockquote><p>
The wording looks good.

Move to Tentatively Ready.
</p></blockquote>

<p><i>[
Adopted at 2010-11 Batavia
]</i></p>




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

<ol>

<li>
<p>
Change  [forwardlist.ops]/12 as indicated:
</p>

<blockquote><pre>
void remove(const T&amp; value);
template &lt;class Predicate&gt; void remove_if(Predicate pred);
</pre>

<blockquote><p>
12 <i>Effects:</i> Erases all the elements in the list referred by a list
iterator <code>i</code> for which the following conditions hold: <code>*i == value
(for remove()), pred(*i)</code> is true (<code>for remove_if()</code>). This operation
shall be stable: the relative order of the elements that are not removed is the
same as their relative order in the original list. <ins>Invalidates only the
iterators and references to the erased elements.</ins>
</p></blockquote>
</blockquote>
</li>

<li>
<p>
Change  [forwardlist.ops]/15 as indicated:
</p>

<blockquote><pre>
template &lt;class BinaryPredicate&gt; void unique(BinaryPredicate pred);
</pre>

<blockquote><p>
15 <i>Effects:</i>: <del>Eliminates</del><ins>Erases</ins> all but the first
element from every consecutive group of equal elements referred to by the
iterator <code>i</code> in the range <code>[first + 1,last)</code> for which <code>*i ==
*(i-1)</code> (for the version with no arguments) or <code>pred(*i, *(i - 1))</code>
(for the version with a predicate argument) holds. <ins>Invalidates only the
iterators and references to the erased elements.</ins>
</p></blockquote>
</blockquote>
</li>

<li>
<p>
Change  [forwardlist.ops]/19 as indicated:
</p>

<blockquote><pre>
void merge(forward_list&lt;T,Allocator&gt;&amp;&amp; x);
template &lt;class Compare&gt; void merge(forward_list&lt;T,Allocator&gt;&amp;&amp; x, Compare comp)
</pre>

<blockquote>
<p>
[..]
</p>

<p>
19 <i>Effects:</i>: Merges <code>x</code> into <code>*this</code>. This operation shall
be stable: for equivalent elements in the two lists, the elements from
<code>*this</code> shall always precede the elements from <code>x</code>. <code>x</code> is
empty after the merge. If an exception is thrown other than by a comparison
there are no effects. <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>.</ins>
</p>
</blockquote>
</blockquote>
</li>

<li>
<p>
Change  [forwardlist.ops]/22 as indicated:
</p>

<blockquote><pre>
void sort();
template &lt;class Compare&gt; void sort(Compare comp);
</pre>

<blockquote>
<p>
[..]
</p>

<p>
22 <i>Effects:</i>: Sorts the list according to the <code>operator&lt;</code> or the
<code>comp</code> function object. This operation shall be stable: the relative
order of the equivalent elements is preserved. If an exception is thrown the
order of the elements in <code>*this</code> is unspecified. <ins>Does not affect the
validity of iterators and references.</ins>
</p>
</blockquote>
</blockquote>
</li>

<li>
<p>
Change  [forwardlist.ops]/24 as indicated:
</p>

<blockquote><pre>
void reverse();
</pre>

<blockquote><p>
24 <i>Effects:</i>: Reverses the order of the elements in the list. <ins>Does
not affect the validity of iterators and references.</ins>
</p></blockquote>
</blockquote>
</li>

<li>
<p>
Change 23.3.11.5 <a href="https://wg21.link/list.ops">[list.ops]</a>, p15:
</p>

<blockquote><pre>
                           void remove(const T&amp; value);
template &lt;class Predicate&gt; void remove_if(Predicate pred);
</pre>
<blockquote><p>
<i>Effects:</i> Erases all the elements in the list referred by a list iterator
<code>i</code> for which the following conditions hold: <code>*i == value, pred(*i) !=
false</code>.  <ins>Invalidates only the iterators and references to the erased
elements.</ins>
</p></blockquote>
</blockquote>
</li>

<li>
<p>
Change 23.3.11.5 <a href="https://wg21.link/list.ops">[list.ops]</a>, p19:
</p>

<blockquote><pre>
                                 void unique();
template &lt;class BinaryPredicate&gt; void unique(BinaryPredicate binary_pred);
</pre>
<blockquote><p>
<i>Effects:</i> <del>Eliminates</del> <ins>Erases</ins> all but the first
element from every consecutive group of equal elements referred to by the
iterator <code>i</code> in the range <code>[first + 1,last)</code> for which <code>*i ==
*(i-1)</code> (for the version of <code>unique</code> with no arguments) or
<code>pred(*i, *(i - 1))</code> (for the version of <code>unique</code> with a predicate
argument) holds. <ins>Invalidates only the iterators and references to the
erased elements.</ins>
</p></blockquote>
</blockquote>
</li>

<li>
<p>
Change 23.3.11.5 <a href="https://wg21.link/list.ops">[list.ops]</a>, p23:
</p>

<blockquote><pre>
void                          merge(list&lt;T,Allocator&gt;&amp;&amp; x);
template &lt;class Compare&gt; void merge(list&lt;T,Allocator&gt;&amp;&amp; x, Compare comp);
</pre>
<blockquote><p>
<i>Effects:</i> If <code>(&amp;x == this)</code> does nothing; otherwise, merges the
two sorted ranges <code>[begin(), end())</code> and <code>[x.begin(), x.end())</code>.
The result is a range 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 false.
<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>.</ins>
</p></blockquote>
</blockquote>
</li>

<li>
<p>
Change 23.3.11.5 <a href="https://wg21.link/list.ops">[list.ops]</a>, p26:
</p>

<blockquote><pre>
void reverse();
</pre>
<blockquote><p>
<i>Effects:</i> Reverses the order of the elements in the list.
<ins>Does not affect the validity of iterators and references.</ins>
</p></blockquote>
</blockquote>
</li>

<li>
<p>
Change 23.3.11.5 <a href="https://wg21.link/list.ops">[list.ops]</a>, p30:
</p>

<blockquote><pre>
                         void sort();
template &lt;class Compare&gt; void sort(Compare comp);
</pre>
<blockquote><p>
<i>Effects:</i> Sorts the list according to the <code>operator&lt;</code> or a
<code>Compare</code> function object.
<ins>Does not affect the validity of iterators and references.</ins>
</p></blockquote>
</blockquote>
</li>

</ol>






</body>
</html>
