<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2366: istreambuf_iterator end-of-stream equality</title>
<meta property="og:title" content="Issue 2366: istreambuf_iterator end-of-stream equality">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2366.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="2366"><a href="lwg-active.html#2366">2366</a>. <code>istreambuf_iterator</code> end-of-stream equality</h3>
<p><b>Section:</b> 24.6.4 <a href="https://wg21.link/istreambuf.iterator">[istreambuf.iterator]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Hyman Rosen <b>Opened:</b> 2014-02-19 <b>Last modified:</b> 2023-04-13</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#istreambuf.iterator">active issues</a> in [istreambuf.iterator].</p>
<p><b>View all other</b> <a href="lwg-index.html#istreambuf.iterator">issues</a> in [istreambuf.iterator].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Given the following code,
</p>
<blockquote><pre>
#include &lt;sstream&gt;

std::stringbuf buf;
std::istreambuf_iterator&lt;char&gt; begin(&amp;buf);
std::istreambuf_iterator&lt;char&gt; end;
</pre></blockquote>
<p>
it is not clear from the wording of the Standard whether <code>begin.equal(end)</code>
must be true. In at least one implementation it is not (CC: Sun C++ 5.10 SunOS_sparc Patch 128228-25 2013/02/20) and in at least
one implementation it is (gcc version 4.3.2 x86_64-unknown-linux-gnu).
<p/>
24.6.4 <a href="https://wg21.link/istreambuf.iterator">[istreambuf.iterator]</a> says that <code>end</code> is an end-of-stream iterator since it was default
constructed. It also says that an iterator becomes equal to an end-of-stream
iterator when end of stream is reached by <code>sgetc()</code> having returned <code>eof()</code>.
 [istreambuf.iterator::equal] says that <code>equal()</code> returns true iff both iterators are end of stream
or not end of stream. But there seems to be no requirement that <code>equal</code> check for end-of-stream by calling <code>sgetc()</code>. 
<p/>
Jiahan Zi at BloombergLP discovered this issue through his code failing to
work correctly. Dietmar K&uuml;hl has opined in a private communication that
the iterators should compare equal.
</p>

<p><i>[2023-03-31; Jonathan Wakely comments]</i></p>

<p>
I agree that they should compare equal, but that's in conflict with the
resolution of LWG <a href="lwg-defects.html#2544" title="istreambuf_iterator(basic_streambuf&lt;charT, traits&gt;* s) effects unclear when s is 0 (Status: C++17)">2544</a><sup><a href="https://cplusplus.github.io/LWG/issue2544" title="Latest snapshot">(i)</a></sup>, which says that <code>begin</code>
must not be at end-of-stream because <code>&amp;buf</code> is not null.
</p>

<p><i>[2023-04-12; Jonathan adds wording]</i></p>




<p id="res-2366"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/N4944" title=" Working Draft, Standard for Programming Language C++">N4944</a>.</p>

<ol>
<li>
<p>Change 24.6.4.1 <a href="https://wg21.link/istreambuf.iterator.general">[istreambuf.iterator.general]</a> as indicated:</p>
<blockquote>
<pre><code>
    constexpr istreambuf_iterator() noexcept;
    constexpr istreambuf_iterator(default_sentinel_t) noexcept;
    istreambuf_iterator(const istreambuf_iterator&amp;) noexcept = default;
    ~istreambuf_iterator() = default;
    istreambuf_iterator(istream_type&amp; s) noexcept<del>;</del>
    <ins>: istreambuf_iterator(s.rdbuf()) { }</ins>
    istreambuf_iterator(streambuf_type* s) noexcept;
    istreambuf_iterator(const <i>proxy</i>&amp; p) noexcept;
    <i>&hellip;</i>

  private:
    streambuf_type* sbuf_;              <i>// exposition only</i>
    <ins>int_type c_{};                      <i>// exposition only</i></ins>
  };
}
</code></pre>
</blockquote>
</li>
<li>
<p>Change 24.6.4.3 <a href="https://wg21.link/istreambuf.iterator.cons">[istreambuf.iterator.cons]</a> as indicated:</p>
<blockquote>
<p>
For each <code>istreambuf_iterator</code> constructor in this section, an end-of-stream iterator is
constructed if <del>and only if</del> the exposition-only member <code>sbuf_</code> is initialized with a null
pointer value
<ins>or if <code>sbuf_->sgetc()</code> returns <code>traits_type::eof()</code></ins>.
</p>
<pre>
constexpr istreambuf_iterator() noexcept;
constexpr istreambuf_iterator(default_sentinel_t) noexcept;
</pre>
<blockquote>
<p>
-1- <i>Effects</i>: Initializes <code>sbuf_</code> with <code>nullptr</code>.
</p>
</blockquote>
<pre><del>istreambuf_iterator(istream_type&amp; s) noexcept;</del></pre>
<blockquote>
<p><del>
-2- <i>Effects</i>: Initializes <code>sbuf_</code> with <code>s.rdbuf()</code>.
</del></p>
</blockquote>
<pre>
istreambuf_iterator(streambuf_type* s) noexcept;
</pre>
<blockquote class="note">
<p>
[<i>Drafting note:</i>
<code>sgetc()</code> can throw, but this function is <code>noexcept</code>.
Should it swallow exceptions and create an end-of-stream iterator,
to avoid weakening the exception spec of an existing function?]
</p>
</blockquote>
<blockquote>
<p>
-3- <i>Effects</i>: Initializes <code>sbuf_</code> with <code>s</code>.
<ins>
If <code>s</code> is not null, initializes <code>c_</code> with <code>s-&gt;sgetc()</code>.
Sets <code>sbuf_</code> to null if <code>sgetc</code> exits via an exception,
or if <code>traits_type::eq_int_type(c_, traits_type::eof())</code> is <code>true</code>.
</ins>
</p>
</blockquote>
<pre>
istreambuf_iterator(const proxy&amp; p) noexcept;
</pre>
<blockquote>
<p>
-4- <i>Effects</i>: Initializes <code>sbuf_</code> with <code>p.sbuf_</code>.
<ins>
If <code>p.sbuf_</code> is not null, initializes <code>c_</code> with <code>p.keep_</code>.
</ins>
</p>
</blockquote>
</blockquote>
</li>
<li>
<p>Change 24.6.4.4 <a href="https://wg21.link/istreambuf.iterator.ops">[istreambuf.iterator.ops]</a> as indicated:</p>
<blockquote>
<pre>
charT operator*() const;
</pre>
<blockquote>
<p>
<ins>-?- <i>Preconditions</i>: <code>sbuf_</code> is not null.</ins>
</p>
<p>
-1- <i>Returns</i>:
<del>
The character obtained via the <code>streambuf</code>
member <code>sbuf_-&gt;sgetc()</code>.
</del>
<ins><code>traits_type::to_char_type(c_)</code>.</ins>
</p>
<p>
<ins>-?- <i>Throws</i>: Nothing.</ins>
</p>
</blockquote>
<pre>
istreambuf_iterator&amp; operator++();
</pre>
<blockquote>
<p>
<ins>-?- <i>Preconditions</i>: <code>sbuf_</code> is not null.</ins>
</p>
<p>-2-
<i>Effects</i>:
<del>As if by <code>sbuf_-&gt;sbumpc()</code>.</del>
<ins>
Performs <code>c_ = sbuf_-&gt;snextc()</code>,
then sets <code>sbuf_</code> to null
if <code>traits_type::eq_int_type(c_, traits_type::eof())</code> is <code>true</code>.
</ins>
</p>
<p>-3-
<i>Returns</i>: <code>*this</code>.
</p>
</blockquote>
<pre>
<i>proxy</i> operator++(int);
</pre>
<blockquote>
<p>-4-
<del>
<i>Returns</i>: <code><i>proxy</i>(sbuf_-&gt;sbumpc(), sbuf_)</code>.
</del>
<br/>
<ins><i>Effects</i>: Equivalent to:</ins>
<pre><ins><code><i>proxy</i> p(**this, sbuf_);
++*this;
return p;
</code></ins></pre>
</p>
</blockquote>
<pre>
bool equal(const istreambuf_iterator&amp; b) const;
</pre>
<blockquote>
<p>-5-
<i>Returns</i>: <ins><code>bool(sbuf_) == bool(b.sbuf_)</code>.</ins>
</p>
<p>
<ins>[<i>Note</i>: This is</ins>
<code>true</code> if and only if both iterators are at end-of-stream,
or neither is at end-of-stream, regardless of what streambuf object they use.
<ins>&mdash; <i>end note</i>]</ins>
</p>
</blockquote>
<pre>
template&lt;class charT, class traits&gt;
  bool operator==(const istreambuf_iterator&lt;charT, traits&gt;&amp; a,
                  const istreambuf_iterator&lt;charT, traits&gt;&amp; b);
</pre>
<blockquote>
<p>-6-
<i>Returns</i>: <code>a.equal(b)</code>.
</p>
</blockquote>
<pre>
bool equal(const istreambuf_iterator&amp; i, default_sentinel_t s) const;
</pre>
<blockquote>
<p>-7-
<i>Returns</i>: <code><del>i.equal(s)</del> <ins>i.sbuf_ == nullptr</ins></code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
