<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 765: More on iterator validity</title>
<meta property="og:title" content="Issue 765: More on iterator validity">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue765.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="765"><a href="lwg-defects.html#765">765</a>. More on iterator validity</h3>
<p><b>Section:</b> 24.3.4 <a href="https://wg21.link/iterator.concepts">[iterator.concepts]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Martin Sebor <b>Opened:</b> 2007-12-14 <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#iterator.concepts">issues</a> in [iterator.concepts].</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>

Issue <a href="lwg-defects.html#278" title="What does iterator validity mean? (Status: CD1)">278</a><sup><a href="https://cplusplus.github.io/LWG/issue278" title="Latest snapshot">(i)</a></sup>
defines the meaning of the term "invalid iterator" as one that may be
singular.

       </p>
       <p>

Consider the following code:

       </p>
       <pre>
   std::deque&lt;int&gt; x, y;
   std::deque&lt;int&gt;::iterator i = x.end(), j = y.end();
   x.swap(y);
       </pre>
       <p>

Given that <code>swap()</code> is required not to invalidate iterators
and using the definition above, what should be the expected result of
comparing <code>i</code> and <code>j</code> to <code>x.end()</code>
and <code>y.end()</code>, respectively, after the <code>swap()</code>?

       </p>
       <p>

I.e., is the expression below required to evaluate
to <code>true</code>?

       </p>
       <pre>
   i == y.end() &amp;&amp; j == x.end()
       </pre>
       <p>

(There are at least two implementations where the expression
returns <code>false</code>.)

       </p>
       <p>

More generally, is the definition introduced in issue <a href="lwg-defects.html#278" title="What does iterator validity mean? (Status: CD1)">278</a><sup><a href="https://cplusplus.github.io/LWG/issue278" title="Latest snapshot">(i)</a></sup> meant to
make any guarantees about whether iterators actually point to the same
elements or be associated with the same containers after a
non-invalidating operation as they did before?

       </p>
       <p>

Here's a motivating example intended to demonstrate the importance of
the question:

       </p>
       <pre>
   Container x, y ({ 1, 2});   // pseudocode to initialize y with { 1, 2 }
   Container::iterator i = y.begin() + 1;
   Container::iterator j = y.end();
   std::swap(x, y);
   std::find(i, j, 3);
       </pre>
       <p>

<code>swap()</code> guarantees that <code>i</code> and <code>j</code>
continue to be valid. Unless the spec says that even though they are
valid they may no longer denote a valid range the code above must be
well-defined. Expert opinions on this differ as does the behavior of
popular implementations for some standard <code>Containers</code>.

       </p>
<p><i>[
San Francisco:
]</i></p>


<blockquote>
<p>
Pablo: add a note to the last bullet of paragraph 11 of 23.1.1 clarifying that 
the <code>end()</code> iterator doesn't refer to an element and that it can therefore be invalidated.
</p>
<p>
Proposed wording:
</p>
<blockquote><p>
[<i>Note:</i> The <code>end()</code> iterator does not refer to any element and can
therefore be invalidated. <i>-- end note</i>]
</p></blockquote>
<p>
Howard will add this proposed wording to the issue and then move it to Review.
</p>
</blockquote>

<p><i>[
Post Summit:
]</i></p>


<blockquote>
<p>
Lawrence: suggestion: "Note: The <code>end()</code> iterator does not refer to any element"
</p>
<p>
Walter: "Note: The <code>end()</code> iterator can nevertheless be invalidated,
because it does not refer to any element."
</p>
<p>
Nick: "The <code>end()</code> iterator does not refer to any element. It is therefore
subject to being invalidated."
</p>
<p>
Consensus: go with Nick
</p>
<p>
With that update, Recommend Tentatively Ready.
</p>
</blockquote>



<p id="res-765"><b>Proposed resolution:</b></p>
<p>
Add to 23.2.2 <a href="https://wg21.link/container.requirements.general">[container.requirements.general]</a>, p11:
</p>

<blockquote>
<p>
Unless otherwise specified (see 23.1.4.1, 23.1.5.1, 23.2.2.3, and
23.2.6.4) all container types defined in this Clause meet the following
additional requirements:
</p>
<ul>
<li>...</li>
<li>
no <code>swap()</code> function invalidates any references, pointers, or
iterators referring to the elements of the containers being swapped.
<ins>[<i>Note:</i> The <code>end()</code> iterator does not refer to any element. 
It is therefore subject to being invalidated. &mdash; <i>end note</i>]</ins>
</li>
</ul>
</blockquote>





</body>
</html>
