GCC 4.7 istream::tellg() returns -1 after reaching EOF

后端 未结 2 1077
小蘑菇
小蘑菇 2021-01-18 08:22

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

#include 
#include 
#include 

        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-18 08:53

    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.

提交回复
热议问题