The behavior you seem to expect is probably wrong. Both C++11
and C++03 start the description of tellg
with "Behaves as an
unformatted input function[...]". An "unformatted input
function" starts by constructing a sentry
object, and will
fail, doing nothing and returning a failure status, if the
sentry
object converts to false
. And the sentry
object
will convert to false
if the eofbit
is set.
The standard is slightly less clear about whether reading the
number sets the eofbit
, but only slightly (with the
information spread out over several different sections).
Basically, when inputting a numeric value, the stream (actually,
the num_get
facet) must read one character ahead, in order to
know where the number ends. In your case, it will see the end
of file when this occurs, and will thus set eofbit
. So your
first assert
will fail with a conforming implementation.
One could very easily consider this a defect in the standard, or unintentional. It's very easy to imagine some implementations doing the sensible thing (which is what you seem to expect), perhaps because the original implementors didn't realize the full implications in the standard (or unconsciously read it as they thought it should read). I would guess that this is the case for g++, and when they realized that their behavior was non-conformant, they fixed it.
As for work-arounds... I'm not sure what the real problem is,
that you're trying to work around. But I think that if you
clear the error bits before the tellg
, it should work. (Of
course, then iScan.good()
will be true
, and iScan.eof()
false
. But can this really matter?) Just be sure to check
that the extraction actually succeeded before you clear the
status.