From: Bart van Ingen Schenau on
johnehein(a)gmail.com wrote:

> #include <iostream>
> #include <sstream>
> using namespace std;
>
> static void
> pbits(const stringstream & ss)
> {
> cerr << "bad: " << ss.bad() << ".\n";
> cerr << "eof: " << ss.eof() << ".\n";
> cerr << "fail: " << ss.fail() << ".\n";
> cerr << "good: " << ss.good() << ".\n";
> cerr << "========\n";
> }
>
> int
> main()
> {
> stringstream ss;
> char c;
> int i1, i2;
>
> ss << "1,2";
> ss >> i1; pbits(ss);
> ss >> c; pbits(ss);
> ss >> i2; pbits(ss);
> ss >> c; pbits(ss);
> return 0;
> }
>
>
> Given the above program, I get two different results from two
> different c++ implementations. The output of the two different runs
> is shown below. Notice the behavior after reading the last valid
> character, "2", is different (the last "ss >> c" is bogus, I know, but
> I just added to see how the two implementations would differ).
>
> Which one is correct? And where is it documented what to expect? Can
> I portably code to work with either?

The first implementation (that sets eof after the 'ss >> i2;' statement,
is conforming.
This is documented in the standard in [lib.facet.nu.get.virtuals]/13.

Code that terminates the read-loop only when it fails to read something
will work portably with both implementations.
The tests
if (stream)
and
if (!stream)
only test for the fail-bit, which only gets set on the last 'ss >> c;'
statement, so if your code is constructed around that idiom, it will
work with both implementations.

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]