6

The following code works with gcc 4.4.
But gcc 4.7 will give assertion failure.

#include <assert.h>
#include <iostream>
#include <sstream>

using namespace std;

int main()
{

    string input("abcdefg");
    stringstream iss(input);
    ostringstream oss;
    oss << iss.rdbuf();

    assert (!iss.eof());
    (void) iss.peek();
    assert (iss.eof());

    // the following assertion will fail with gcc 4.7
    assert( streamoff(iss.tellg()) ==
            streamoff(input.length()) );

    return 0;
}

In gcc 4.7, if the istream has reached EOF, tellg() will return -1. no pubseekoff() nor seekoff() will be called In gcc 4.4 it is not a problem.

Which is the supposed to be behavior, gcc 4.4 or gcc 4.7? Why?

John Crane
  • 371
  • 5
  • 14
  • A little confused. Why isn't `iss.eof()` set after extraction? – Ben Voigt Dec 05 '12 at 21:12
  • It was fixed from gcc 4.6, here [is the bug report](http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26211). – Jesse Good Dec 05 '12 at 21:35
  • That bug report does not have too much description. The files were changed are related with the current issue though. How did you associate that bug fix with the current issue? – John Crane Dec 06 '12 at 20:25

2 Answers2

5

According to C++11 section 27.7.2.3p40,

if fail() != false, returns pos_type(-1)

So gcc 4.7 has the correct behavior for the current version of C++ (assuming that peek() at end of stream causes failbit to be set, and it does during sentry construction, since skipws is set by default).

Looking at the wording of C++03, it is the same. 27.6.1.3p37. So the behavior you describe in gcc 4.4 is a bug.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
2

To be precise, eofbit won't cause tellg() to return -1. But the fact that you read past EOF sets the failbit, and tellg() will return -1 if badbit or failbit are set.

The solution is to clear the status flags before calling tellg():

iss.clear();
iss.tellg();  // should work
rustyx
  • 80,671
  • 25
  • 200
  • 267