Reference <ios> ios operator bool
public member function
<ios> <iostream> std::ios::operator booloperator void*() const;
explicit operator bool() const;Evaluate stream
Returns whether an error flag is set (either failbit or badbit).
Notice that this function does not return the same as member good, but the opposite of member fail.
The function returns a null pointer if at least one of these error flags is set, and some other value otherwise.
The function returns false if at least one of these error flags is set, and true otherwise.
Parameters
none
Return Value
A null pointer if at least one of failbit or badbit is set. Some other value otherwise.
true if none of failbit or badbit is set.
false otherwise.
Example
123456789101112131415 // evaluating a stream
#include <iostream> // std::cerr
#include <fstream> // std::ifstream
int main () {
std::ifstream is;
is.open ("test.txt");
if (is) {
// read file
}
else {
std::cerr << "Error opening 'test.txt'\n";
}
return 0;
}
Data races
Accesses the stream object.
Concurrent access to the same stream object may cause data races.
Exception safety
Strong guarantee: if an exception is thrown, there are no changes in the stream.
See also
ios::failCheck whether either failbit or badbit is set (public member function )ios::operator!Evaluate stream (not) (public member function )