Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

why iostream sentry doesn't set failbit when it has reached the end

19 views
Skip to first unread message

Shiyao MA

unread,
Oct 2, 2017, 8:09:19 PM10/2/17
to
or the following code:

#include <iostream>
#include <sstream>

using namespace std;

int main() {
istringstream iss("a");
iss.get();
cout << iss.tellg() << endl; // 1
cout << iss.fail() << endl; // 0
}
I'd expect the result be -1,1 not 1, 0.

tellg will first construct a sentry and then check fail().

according to http://eel.is/c++draft/istream::sentry#2
If is.rdbuf()->sbumpc() or is.rdbuf()->sgetc() returns traits​::​eof(), the function calls setstate(failbit | eofbit) (which may throw ios_­base​::​failure).


so fail() should be true, and tellg should return -1.

0 new messages