This page is a snapshot from the LWG issues list, see theLibrary Active Issues List for more information and the meaning ofC++11 status.
failbit ifeofbit is already setSection: 31.7.5.2.4[istream.sentry]Status:C++11Submitter: Martin SeborOpened: 2003-09-18Last modified: 2021-06-06
Priority:Not Prioritized
View all otherissues in [istream.sentry].
View all issues withC++11 status.
Discussion:
[istream::sentry], p2 says thatistream::sentry ctor prepares for input ifis.good()is true. p4 then goes on to say that the ctor sets thesentry::ok_ member totrue if the stream state is good after any preparation. 31.7.5.3.1[istream.formatted.reqmts], p1 thensays that a formatted input function endeavors to obtain the requested inputif the sentry'soperator bool() returns true.Given these requirements, no formatted extractor should ever setfailbit ifthe initial streamrdstate() == eofbit. That is contrary to the behavior ofall implementations I tested. The program below prints out
eof = 1, fail = 0eof = 1, fail = 1
on all of them.
#include <sstream>#include <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 ());}
Comments from Jerry Schwarz (c++std-lib-11373):
Jerry Schwarz wrote:
I don't know where (if anywhere) it says it in the standard, but theformatted extractors are supposed to setfailbit if they don't extractany characters. If they didn't then simple loops like
while (cin >> x);
would loop forever.
Further comments from Martin Sebor:
The question is which part of the extraction should prevent this from happeningby settingfailbit wheneofbit is already set. It could either be the sentryobject or the extractor. It seems that most implementations have chosen tosetfailbit in the sentry [...] so that's the text that will need to becorrected.
Pre Berlin: This issue is related to342(i). If the sentrysetsfailbit when it findseofbit already set, thenyou can never seek away from the end of stream.
Kona: Possibly NAD. Ifeofbit is set thengood() will return false. We then setok to false. We believe that the sentry's constructor should always setfailbit whenok is false, and we also think the standard already says that. Possibly it could be clearer.
[2009-07 Frankfurt]
Moved to Ready.
Proposed resolution:
Change [istream::sentry], p2 to:
explicit sentry(basic_istream<charT,traits>&is , boolnoskipws = false);-2-Effects: If
is.good()istruefalse,callsis.setstate(failbit).Otherwise prepares for formatted or unformatted input. ...