I've read that the following is an anti-pattern:
while(in.good()) {
// ...
}
But this is preferred:
while(operation) {
}
However, the stream has a conversion operator to bool that returns !fail()
. Isn't !fail()
the same as good()
? If not, why aren't these two functions symmetrical? I'd expect it to be similar to true == !false
.
There's an excellent table at http://en.cppreference.com/w/cpp/io/basic_ios/good that explains state of an std::istream
and the values various functions return.
The only time while ( stream.good() )
and while(stream)
will be different is when the contents of stream have been successfully read and EOF has been reached. At that time, stream.good()
returns true
while (bool)stream
evaluates to false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With