Consider the following code:
#include <iostream>
#include <sstream>
void print_flags(std::istringstream & iss)
{
std::cout << "good: " << iss.good() << ", eof: " << iss.eof() << ", fail: " << iss.fail() << ", bad: " << iss.bad() << "\n";
}
int main()
{
std::istringstream iss("A");
char chr;
iss >> chr;
print_flags(iss);
iss.peek();
print_flags(iss);
}
On both g++ and clang++ I see the following output:
good: 1, eof: 0, fail: 0, bad: 0
good: 0, eof: 1, fail: 0, bad: 0
The cppreference article does not mention that peek modifies the flags. It mentions the flags being modified by get: cppreference.
Is this behaviour correct with regards to the standard?
Yes, it's a requirement on UnformattedInputFunction.
First, description of
peek(emphasis mine):Then, from description of UnformattedInputFunction above (emphasis mine):