<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3308: vector and deque iterator erase invalidates elements even when no change occurs</title>
<meta property="og:title" content="Issue 3308: vector and deque iterator erase invalidates elements even when no change occurs">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3308.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="3308"><a href="lwg-active.html#3308">3308</a>. <code>vector</code> and <code>deque</code> iterator <code>erase</code> invalidates elements even when no change occurs</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#New">New</a>
 <b>Submitter:</b> Billy O'Neal III <b>Opened:</b> 2019-10-29 <b>Last modified:</b> 2019-11-04</p>
<p><b>Priority: </b>3
</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#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
It seems incorrect that a container would invalidate anything as a result of being asked to erase 0 elements. 
This came up in a recent customer bug report against Visual Studio, where given a <code>vector v</code>, 
<code>v.erase(v.begin(), v.begin())</code> triggered a self-assignment of all the elements in the vector. 
<p/>
<code>deque</code> has language enumerating erasures of the first and last element which invalidate fewer 
iterators, and a fallback that says all iterators are invalidated, which seems to intend to be talking about middle-of-container erasures. However, erasing 0 elements isn't really a middle of container erasure.
<p/>
<code>vector</code> says that iterators and references are invalidated after the 'point of the erase', but when 0 
elements are erased it's unclear what that even means.
<p/>
We should say that erasures that erase 0 elements are no ops and be clearer about which elements are 
invalidated for <code>vector</code>.
</p>

<p><i>[2019-11 Priority to 3 during Monday issue prioritization in Belfast]</i></p>



<p id="res-3308"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4835">N4835</a>.</p>

<ol>
<li><p>Modify 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);
void pop_front();
void pop_back();
</pre>
<blockquote>
<p>
-4- <i>Effects:</i> <ins>Erases elements as indicated in Table 75 [tab:container.seq.req].</ins> 
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 iterators and references 
to the erased elements. An erase operation that erases <ins>any elements, but</ins> 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. <del>[<i>Note:</i> <code>pop_front</code> 
and <code>pop_back</code> are erase operations. &mdash; <i>end note</i>]</del>
<p/>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
constexpr iterator erase(const_iterator position);
constexpr iterator erase(const_iterator first, const_iterator last);
constexpr void pop_back();
</pre>
<blockquote>
<p>
-3- <i>Effects:</i> <ins>Erases elements as indicated in Table 75 [tab:container.seq.req].</ins> 
Invalidates iterators and references at or after the <del>point of the erase.</del><ins>first erased 
element. [<i>Note:</i> For the second overload of <code>erase</code>, if <code>first == last</code>, no 
elements are erased, and no iterators or references are invalidated. &mdash; <i>end note</i>]</ins>
<p/>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
