<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 419: istream extractors not setting failbit if eofbit is already set</title>
<meta property="og:title" content="Issue 419: istream extractors not setting failbit if eofbit is already set">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue419.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="419"><a href="lwg-defects.html#419">419</a>. istream extractors not setting <code>failbit</code> if <code>eofbit</code> is already set</h3>
<p><b>Section:</b> 31.7.5.2.4 <a href="https://wg21.link/istream.sentry">[istream.sentry]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Martin Sebor <b>Opened:</b> 2003-09-18 <b>Last modified:</b> 2021-06-06</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#istream.sentry">issues</a> in [istream.sentry].</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>

 [istream::sentry], p2 says that <code>istream::sentry</code> ctor prepares for input if <code>is.good()</code>
is true. p4 then goes on to say that the ctor sets the <code>sentry::ok_</code> member to
true if the stream state is good after any preparation. 31.7.5.3.1 <a href="https://wg21.link/istream.formatted.reqmts">[istream.formatted.reqmts]</a>, p1 then
says that a formatted input function endeavors to obtain the requested input
if the sentry's <code>operator bool()</code> returns true.

Given these requirements, no formatted extractor should ever set <code>failbit</code> if
the initial stream <code>rdstate() == eofbit</code>. That is contrary to the behavior of
all implementations I tested. The program below prints out
</p>
<blockquote><pre>
eof = 1, fail = 0
eof = 1, fail = 1
</pre></blockquote>
<p>
on all of them.
        </p>
<pre>

#include &lt;sstream>
#include &lt;cstdio>

int main()
{
    std::istringstream strm ("1");

    int i = 0;

    strm >> i;

    std::printf ("eof = %d, fail = %d\n",
                 !!strm.eof (), !!strm.fail ());

    strm >> i;

    std::printf ("eof = %d, fail = %d\n",
                 !!strm.eof (), !!strm.fail ());
}

</pre>
        <p>
<br/>

Comments from Jerry Schwarz (c++std-lib-11373):
<br/>

Jerry Schwarz wrote:
<br/>

I don't know where (if anywhere) it says it in the standard, but the
formatted extractors are supposed to set <code>failbit</code> if they don't extract
any characters. If they didn't then simple loops like
<br/>

while (cin >> x);
<br/>

would loop forever.
<br/>

Further comments from Martin Sebor:
<br/>

The question is which part of the extraction should prevent this from happening
by setting <code>failbit</code> when <code>eofbit</code> is already set. It could either be the sentry
object or the extractor. It seems that most implementations have chosen to
set <code>failbit</code> in the sentry [...] so that's the text that will need to be
corrected.

        </p>
<p>
Pre Berlin:  This issue is related to <a href="lwg-closed.html#342" title="seek and eofbit (Status: NAD)">342</a><sup><a href="https://cplusplus.github.io/LWG/issue342" title="Latest snapshot">(i)</a></sup>.  If the sentry
sets <code>failbit</code> when it finds <code>eofbit</code> already set, then
you can never seek away from the end of stream.
</p>
<p>Kona: Possibly NAD.  If <code>eofbit</code> is set then <code>good()</code> will return false.  We
  then set <i>ok</i> to false.  We believe that the sentry's
  constructor should always set <code>failbit</code> when <i>ok</i> is false, and
  we also think the standard already says that.  Possibly it could be
  clearer.</p>


<p><i>[
2009-07 Frankfurt
]</i></p>


<blockquote><p>
Moved to Ready.
</p></blockquote>



<p id="res-419"><b>Proposed resolution:</b></p>
<p>
Change  [istream::sentry], p2 to:
</p>

<blockquote>
<pre>explicit sentry(basic_istream&lt;charT,traits&gt;&amp; <i>is</i> , bool <i>noskipws</i> = false);</pre>
<p>
-2- <i>Effects:</i> If <code>is.good()</code> is <del><code>true</code></del>
<ins><code>false</code></ins>, <ins>calls <code>is.setstate(failbit)</code>.
Otherwise</ins> prepares for formatted or unformatted input. ...
</p>
</blockquote>






</body>
</html>
