I/O manipulators | ||||
Print functions(C++23) | ||||
C-style I/O | ||||
Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
Streams | ||||
Abstractions | ||||
File I/O | ||||
String I/O | ||||
Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
Synchronized Output | ||||
(C++20) | ||||
Types | ||||
Error category interface | ||||
(C++11) | ||||
(C++11) |
Member functions | ||||
State functions | ||||
basic_ios::operator bool | ||||
Formatting | ||||
Miscellaneous | ||||
Protected member functions | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) |
operator/* unspecified-boolean-type */()const; | (1) | (until C++11) |
explicit operatorbool()const; | (2) | (since C++11) |
Checks whether the stream has no errors.
This operator makes it possible to use streams and functions that return references to streams as loop conditions, resulting in the idiomatic C++ input loops such aswhile(stream>> value){...} orwhile(std::getline(stream, string)){...}. Such loops execute the loop's body only if the input operation succeeded.
Contents |
(none)
This conversion can be used in contexts where abool is expected (e.g. anif condition). However,implicit conversions (e.g. toint) that can occur withbool are not allowed.
In C++98,operatorbool could not be provided directly due tothe safe bool problem. The initial solution in C++98 is to provideoperatorvoid*, which returns a null pointer iffail() returnstrue or a non-null pointer otherwise. It is replaced by the resolution ofLWG issue 468, which allowsSafe Bool idiom to be applied.
Since C++11, conversion functions can beexplicit. The resolution ofLWG issue 1094 introduced the explicitoperatorbool and the boolean conversion is now safe.
#include <iostream>#include <sstream> int main(){std::istringstream s("1 2 3 error");int n; std::cout<<std::boolalpha<<"s is "<<static_cast<bool>(s)<<'\n';while(s>> n)std::cout<< n<<'\n';std::cout<<"s is "<<static_cast<bool>(s)<<'\n';}
Output:
s is true123s is false
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 468 | C++98 | operatorvoid* was provided | a conversion function to an unspecified boolean type is provided instead |
The following table shows the value ofbasic_ios accessors (good(),fail(), etc.) for all possible combinations ofios_base::iostate flags:
ios_base::iostate flags | basic_ios accessors | |||||||
eofbit | failbit | badbit | good() | fail() | bad() | eof() | operator bool | operator! |
false | false | false | true | false | false | false | true | false |
false | false | true | false | true | true | false | false | true |
false | true | false | false | true | false | false | false | true |
false | true | true | false | true | true | false | false | true |
true | false | false | false | false | false | true | true | false |
true | false | true | false | true | true | true | false | true |
true | true | false | false | true | false | true | false | true |
true | true | true | false | true | true | true | false | true |