0

In the below code, we call in.get(c) repeatedly and it returns an isteam& each time. How does an instance of isteam& evaluate to false in the while statement?

I checked that in.get isn't returning a null reference so I'm not sure how else it could evaluate to false.

istream &to_std_out(istream &in) {
    char c;
    while (in.get(c)) {
        cout << c;
    }
    in.clear();
    return in;
}

int main() {
    istringstream str("A sailor went to see, see, see.");
    to_std_out(str);
}
countunique
  • 3,475
  • 5
  • 21
  • 32

1 Answers1

1

The streams have a conversion operator to void* or an explicit conversion operator to bool. The stream converts to false if at least one of the state bits std::ios_base::badbit or std:: ios_base::failbit is set.

Dietmar Kühl
  • 141,209
  • 12
  • 196
  • 356