On 7/26/2013 2:37 AM, mathieu wrote:
I'm not sure that the two asserts above are guaranteed to pass
(although it's hard to imagine an implementation where they
didn't).
The requirements have changed between C++03 and C++11. C++03
requires std::istream::seekg to fail if eofbit is set. C++11
says that eofbit will first be reset, before attempting the
seek. (I believe that the behavior specified by C++11 was what
was wanted from the beginning, but C++03 definitly says that
seekg should fail if eofbit is set.)
[...]
Only in C++11. The language has changed in this regard.
Yes. Do you realize that 'eofbit' and 'badbit' are not the same? When
we say "failure", it means the 'badbit' is set.
No. When we say "failure", we mean that either 'failbit' or
'badbit' are set. Most implementations of istream will never
set 'badbit'. (They should, in case there is a real hardware
read error, but they don't.)
'eofbit' is only set
when the reading *reaches* the end of the stream.
More correctly, 'eofbit' is only set when the standard says it
should be set. In general, I would expect it to be set whenever
streambuf::sgetc, streambuf::sbumpc or streambuf::snextc returns
end of file. But the standard is fairly vague about this; it
doesn't mention setting eofbit in the description of operator>>
to a streambuf. One could argue that this means that eofbit
shouldn't be set. (One could also argue that this is an
oversight. C++03 also says that this >> behaves as a formatted
input function, which means that it should skip leading
whitespace. It didn't in classical iostreams, and this has been
corrected in C++11. Perhaps not setting eofbit is a similar
oversight, which just hasn't been corrected yet.)
So, to answer your question, yes, the program is supposed to work. If
it does not (it would be nice to see your explanation of what "does not"
is here), then it is likely due to a bug in the implementation of the
standard I/O library that you're using. <shrug>
His program is based on unspecified behavior at one point, and
behavior which has changed between C++03 and C++11 at another.
My guess is that g++ 4.4 is implementing exactly what is
specified in C++03, and that g++ 4.7 is implementing the C++11
behavior, considering the change as a "correction", and so not
requiring -std=c++11 to get it.