<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1311: multi-pass property of Forward Iterator underspecified</title>
<meta property="og:title" content="Issue 1311: multi-pass property of Forward Iterator underspecified">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1311.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#Resolved">Resolved</a> status.</em></p>
<h3 id="1311"><a href="lwg-defects.html#1311">1311</a>. multi-pass property of Forward Iterator underspecified</h3>
<p><b>Section:</b> 24.3.5.5 <a href="https://wg21.link/forward.iterators">[forward.iterators]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Alisdair Meredith <b>Opened:</b> 2010-02-07 <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#forward.iterators">issues</a> in [forward.iterators].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The following example demonstrates code that would meet the guarantees of a
Forward Iterator, but only permits a single traversal of the underlying
sequence:
</p>

<blockquote><pre>
template&lt; typename ForwardIterator&gt;
struct bad_iterator {
  shared_ptr&lt;ForwardIterator&gt; impl;

  bad_iterator( ForwardIterator iter ) {
     : impl{new ForwardIterator{iter} } 
     {
  }

  auto operator*() const -&gt; decltype(*ForwardIterator{}) {
     return **impl;
  }

  auto operator-&gt;() const -&gt; ForwardIterator {
     return *impl;
  }

  auto operator==(bad_iterator const &amp; rhs) const -&gt; bool {
     return impl == rhs.impl;
  }

  auto operator++() -&gt; bad_iterator&amp; {
     ++(*impl);
     return *this;
  }
  // other operations as necessary...
};
</pre></blockquote>

<p>
Here, we use <code>shared_ptr</code> to wrap a forward iterator, so all iterators
constructed from the same original iterator share the same 'value', and
incrementing any one copy increments all others.
</p>

<p>
There is a missing guarantee, expressed by the following code sequence
</p>

<blockquote><pre>
FwdIter x = seq.begin();  // obtain forward iterator from a sequence
FwdIter y = x;            // copy the iterator
assert(x == y);           // iterators must be the same
++x;                      // increment *just one* iterator
assert(x != y);           // iterators *must now be different*
++y;                      // increment the other iterator
assert(x == y);           // now the iterators must be the same again
</pre></blockquote>

<p>
That inequality in the middle is an essential guarantee.  Note that this list is
simplified, as each assertion should also note that they refer to exactly the
same element <code>(&amp;*x == &amp;*y)</code> but I am not complicating the issue
with tests to support proxy iterators, or value types overloading unary
<code>operator+</code>.
</p>

<p>
I have not yet found a perverse example that can meet this additional
constraint, and not meet the multi-pass expectations of a Forward Iterator
without also violating other Forward Iterator requirements.
</p>

<p>
Note that I do not yet have standard-ready wording to resolve the problem, as
saying this neatly and succinctly in 'standardese' is more difficult.
</p>

<p><i>[
2010 Pittsburgh:  Moved to <del>NAD Editorial</del><ins>Resolved</ins>.  Rationale added below.
]</i></p>




<p><b>Rationale:</b></p>
<p>
Solved by
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3066.html">N3066</a>.
</p>


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





</body>
</html>
