GCC 4.7 istream :: tellg()在到达EOF后返回-1 [英] GCC 4.7 istream::tellg() returns -1 after reaching EOF
问题描述
以下代码适用于gcc 4.4。
但是gcc 4.7会给出断言失败。
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;
}
在gcc 4.7中,如果istream已经达到EOF,tellg -1。没有pubseekoff()也没有seekoff()将被称为
在gcc 4.4它不是一个问题。
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.
这是应该是行为,gcc 4.4 gcc 4.7?为什么?
Which is the supposed to be behavior, gcc 4.4 or gcc 4.7? Why?
推荐答案
根据C ++ 11第27.7.2.3p40节,
According to C++11 section 27.7.2.3p40,
如果
fail()!= false
,则返回pos_type(-1)
因此gcc 4.7对当前版本的C ++有正确的行为(假设 peek / code>在流结束时导致
failbit
被设置,并且在哨兵构建期间,因为 skipws
默认设置)。
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).
看看C ++ 03的写法,它是一样的。 27.6.1.3p37。所以你在gcc 4.4中描述的行为是一个错误。
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.
这篇关于GCC 4.7 istream :: tellg()在到达EOF后返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!