<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2364: deque and vector pop_back don't specify iterator invalidation requirements</title>
<meta property="og:title" content="Issue 2364: deque and vector pop_back don't specify iterator invalidation requirements">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2364.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++17">C++17</a> status.</em></p>
<h3 id="2364"><a href="lwg-defects.html#2364">2364</a>. <code>deque</code> and <code>vector</code> <code>pop_back</code> don't specify iterator invalidation requirements</h3>
<p><b>Section:</b> 23.3.5.4 <a href="https://wg21.link/deque.modifiers">[deque.modifiers]</a>, 23.3.13.5 <a href="https://wg21.link/vector.modifiers">[vector.modifiers]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Deskin Miller <b>Opened:</b> 2014-02-17 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>0
</p>
<p><b>View other</b> <a href="lwg-index-open.html#deque.modifiers">active issues</a> in [deque.modifiers].</p>
<p><b>View all other</b> <a href="lwg-index.html#deque.modifiers">issues</a> in [deque.modifiers].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
I think it's obvious that <code>vector::pop_back</code> invalidates the path-the-end iterator, but I cannot find language that says so to 
my satisfaction in the Standard. N3797 23.2.4 <a href="https://wg21.link/sequence.reqmts">[sequence.reqmts]</a> Table 101 lists <code>a.pop_back()</code> semantics as "Destroys the 
last element", but nowhere do I see this required to invalidate the end iterator (or iterators previously referring to the last element). 
 [container.reqmts.general]/11 states "Unless otherwise specified (either explicitly or by defining a function in terms of 
other functions), invoking a container member function or passing a container as an argument to a library function shall not 
invalidate iterators to, or change the values of, objects within that container." 23.3.13.5 <a href="https://wg21.link/vector.modifiers">[vector.modifiers]</a>/3 says that each 
flavor of <code>vector::erase</code> "Invalidates iterators and references at or after the point of the erase", but <code>pop_back</code> isn't 
discussed, and it wasn't specified in terms of <code>erase</code>.
</p>
<p>
Similarly for <code>std::deque</code>, 23.2.4 <a href="https://wg21.link/sequence.reqmts">[sequence.reqmts]</a> Table 101 and  [container.reqmts.general]/11 both apply. 
Yet 23.3.5.4 <a href="https://wg21.link/deque.modifiers">[deque.modifiers]</a> likewise doesn't discuss <code>pop_back</code> nor <code>pop_front</code>. Furthermore paragraph 4 fails to 
specify the iterator-invalidation guarantees when erasing the first element but not the last.
<p/>
Both <code>std::vector</code> and <code>std::deque</code> are in contrast to <code>std::list</code>, which says in 23.3.11.4 <a href="https://wg21.link/list.modifiers">[list.modifiers]</a>/3 
regarding <code>pop_back</code> (as well as all forms of <code>erase</code>, <code>pop_front</code>, and <code>clear</code>) "Effects: Invalidates only 
the iterators and references to the erased elements."
</p>

<p><i>[2014-06-16 Jonathan comments and improves wording]</i></p>


<p>
I believe this reflects our preferred form discussed earlier,
specifically putting the signatures with the <code>erase</code> signatures, so that
the full specification of <code>erase()</code> applies to the <code>pop_xxx()</code> functions.
This covers the case for <code>deque</code> where <code>pop_front()</code> erases the only
element (which is both the first and last element).
<p/>
Open question: the "erase" wording talks about "An erase operation" &mdash;
are <code>pop_front</code> and <code>pop_back</code> clearly covered by "erase operations"? I
believe so, as 23.3.5.1 <a href="https://wg21.link/deque.overview">[deque.overview]</a>/1 and other places talk about "insert
and erase operations" which covers <code>push</code>/<code>pop</code> functions too. I've added
a note which could be used to clarify that if desired.
</p>

<strong>Previous resolution [SUPERSEDED]:</strong>
<p/>
<blockquote class="note">
<p>This wording is relative to N3936.</p> 

<ol>
<li><p>Change 23.3.5.4 <a href="https://wg21.link/deque.modifiers">[deque.modifiers]</a> as indicated:</p>

<blockquote>
<pre>
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
</pre>
<blockquote><p>
-4- <i>Effects</i>: An erase operation that erases the last element of a deque invalidates only the past-the-end
iterator and all iterators and references to the erased elements. An erase operation that erases the first
element of a deque but not the last element invalidates only <ins>iterators and references to</ins> the erased elements. 
An erase operation that erases neither the first element nor the last element of a deque invalidates the past-the-end iterator
and all iterators and references to all the elements of the deque.
<p/>
-5- [&hellip;]
<p/>
-6- [&hellip;]
</p>
</blockquote>

<pre>
<ins>void pop_front();
void pop_back();</ins>
</pre>
<blockquote><p>
<ins>-?- <i>Effects</i>: <code>pop_front</code> invalidates iterators and references to the first element of the <code>deque</code>. 
<code>pop_back</code> invalidates the past-the-end iterator, and all iterators and references to the last element of the <code>deque</code>.</ins>
</p>
</blockquote>
</blockquote>
</li>

<li><p>Change 23.3.13.5 <a href="https://wg21.link/vector.modifiers">[vector.modifiers]</a> as indicated:</p>
<blockquote>
<blockquote>
<p>
-5- [&hellip;]
</p>
</blockquote>

<pre>
<ins>void pop_back();</ins>
</pre>
<blockquote><p>
<ins>-?- <i>Effects</i>: Invalidates the past-the-end iterator, and iterators and references to the last element of the vector.</ins>
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2014-06-21 Rapperswil]</i></p>

<p>
Tony van Eerd: Would be good to define "an erase operation is ..." somewhere.
<p/>
AM: The containers clause is known to be suboptimal in many ways.
<p/>
Looks good 
</p>

<p><i>[Urbana 2014-11-07: Move to Ready]</i></p>




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

<p>This wording is relative to N3936.</p>

<ol>
<li><p>Change 23.3.5.4 <a href="https://wg21.link/deque.modifiers">[deque.modifiers]</a> as indicated:</p>

<blockquote>
<pre>
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
<ins>void pop_front();
void pop_back();</ins>
</pre>
<blockquote><p>
-4- <i>Effects</i>: An erase operation that erases the last element of a deque invalidates only the past-the-end
iterator and all iterators and references to the erased elements. An erase operation that erases the first
element of a deque but not the last element invalidates only <ins>iterators and references to</ins> the erased elements. 
An erase operation that erases neither the first element nor the last element of a deque invalidates the past-the-end iterator
and all iterators and references to all the elements of the deque. <ins>[<i>Note</i>: <code>pop_front</code> and <code>pop_back</code> 
are erase operations &mdash; <i>end note</i>]</ins>
</p>
</blockquote>
</blockquote>
</li>

<li><p>Change 23.3.13.5 <a href="https://wg21.link/vector.modifiers">[vector.modifiers]</a> as indicated:</p>
<blockquote>
<pre>
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
<ins>void pop_back();</ins>
</pre>
<blockquote><p>
-3- <i>Effects</i>: Invalidates iterators and references at or after the point of the erase.
</p>
</blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
