Consider the following code snippet:
#include <iostream>
#include <sstream>
int main()
{
std::stringstream ss;
ss << "12345";
unsigned short s;
ss >> s;
ss << "foo";
std::cout << std::boolalpha
<< "\nss.eof() = " << ss.eof()
<< "\nss.good() = " << ss.good()
<< "\nss.bad() = " << ss.bad()
<< "\nss.fail() = " << ss.fail()
<< "\nss.str() = " << ss.str();
}
clang++ trunk prints the following result:
ss.eof() = true ss.good() = false ss.bad() = false ss.fail() = false ss.str() = 12345
g++ trunk prints the following result:
ss.eof() = true ss.good() = false ss.bad() = false ss.fail() = true ss.str() = 12345
As you can see, the value of ss.fail()
is different between the two compilers. What does the Standard say regarding the behavior of std::stringstream
in this case? Is it implementation-defined to set failbit
/badbit
when writing to a stream that has already been consumed?