mercredi 1 avril 2020

C++: end-of-file interpretation when using std::cin as a condition

I know that we can use std::cin as a condition, for example, in

while (std::cin >> value)

using std::cin as a condition will call a member function std::ios::operator bool. It says that it "returns whether an error flag is set (either failbit or badbit)", which does not include eofbit. Despite this, passing end-of-file (by Ctrl+d) terminates the loop. Why? Can failbit or badbit also set an eofbit?

I also found this explanation, but in C++ Reference it specifically says that "this function does not return the same as member good"

Aucun commentaire:

Enregistrer un commentaire