I've used statements such as this quite a bit in my C++ programming:
std::string s;
std::ifstream in("my_input.txt");
if(!in) {
std::cerr << "File not opened" << std::endl;
exit(1);
}
while(in >> s) {
// Do something with s
}
What I want to know is, why does this work?
I looked at the return value of operator>>
, and it's an istream
object, not a boolean. How does an istream object somehow get interpreted as a bool value that can be put inside of if
statements and while
loops?