std::basic_...peratorbool(3)
π
std::basic_ios::operatorbool β std::basic_ios::operatorbool
okay bestie, this is a page from the C++ Standard Libary manpage set (std::basic_...peratorbool(3)) and it's genuinely one of those little gems that makes C++ loops not suck. let's get into it π«Ά
β¨ NAME β¨
std::basic_ios::operatorbool β literally std::basic_ios::operatorbool, no notes, that's the whole name π
πΈ Synopsis πΈ
operator /* unspecified-boolean-type */() const; (1) (until C++11)
explicit operator bool() const; (2) (since C++11)
so what does it do? it checks whether the stream has no errors. that's it, that's the vibe check π
- Returns a value that evaluates to
falsein a boolean context iffail()returnstrue, otherwise returns a value that evaluates totruein a boolean context. - Returns
trueif the stream has no errors and is ready for I/O operations. Specifically, returns!fail().
This operator makes it possible to use streams and functions that return references to streams as loop conditions, which is how you get the idiomatic C++ input loops like
while (stream >> value) {...}
or
while (std::getline(stream, string)) {...}
and those loops only run the body if the input operation actually succeeded. so clean, so readable, obsessed π
π Parameters π
(none) β not a single parameter, she takes nothing and still delivers π
π¦ Return value π¦
- A value that evaluates to
truein a boolean context if the stream has no errors, a value that evaluates tofalsein a boolean context otherwise. trueif the stream has no errors,falseotherwise.
β Notes β
This conversion can be used in contexts where a bool is expected (e.g. an if condition). However, implicit conversions (e.g. to int) that can occur with bool are not allowed. (that's the whole point, bestie β no sneaky int conversions π«)
okay little history lesson because it's actually juicy:
In C++98,operator boolcould not be provided directly due to the safe bool problem. The initial solution in C++98 is to provideoperator void*, which returns a null pointer iffail()returnstrueor a non-null pointer otherwise. It is replaced by the resolution of LWG issue 468, which allows Safe Bool idiom to be applied.
Since C++11, conversion functions can beexplicit. The resolution of LWG issue 1094 introduced theexplicit operator booland the boolean conversion is now safe. slay π
π₯ Example π₯
// Run this code
#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 true
1
2
3
s is false
see how it reads the three numbers, hits the word error, the stream goes bad, and the loop just stops? no cap, that's the whole magic trick π§
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. (yes, they went back and fixed history, main character energy fr fr)
LWG 468β Applied to:C++98β Behavior as published:operator void*was provided β Correct behavior: a conversion function to an unspecified boolean type is provided instead
π See also π
The following table shows the value of basic_ios accessors (good(), fail(), etc.) for all possible combinations of ios_base::iostate flags β so here's the whole grid, every single row, no skipping π
ios_base::iostateflags:eofbitfalse,failbitfalse,badbitfalseβgood()true,fail()false,bad()false,eof()false,operator booltrue,operator!falseios_base::iostateflags:eofbitfalse,failbitfalse,badbittrueβgood()false,fail()true,bad()true,eof()false,operator boolfalse,operator!trueios_base::iostateflags:eofbitfalse,failbittrue,badbitfalseβgood()false,fail()true,bad()false,eof()false,operator boolfalse,operator!trueios_base::iostateflags:eofbitfalse,failbittrue,badbittrueβgood()false,fail()true,bad()true,eof()false,operator boolfalse,operator!trueios_base::iostateflags:eofbittrue,failbitfalse,badbitfalseβgood()false,fail()false,bad()false,eof()true,operator booltrue,operator!falseios_base::iostateflags:eofbittrue,failbitfalse,badbittrueβgood()false,fail()true,bad()true,eof()true,operator boolfalse,operator!trueios_base::iostateflags:eofbittrue,failbittrue,badbitfalseβgood()false,fail()true,bad()false,eof()true,operator boolfalse,operator!trueios_base::iostateflags:eofbittrue,failbittrue,badbittrueβgood()false,fail()true,bad()true,eof()true,operator boolfalse,operator!true
tiny heads up bestie:eofbitbeing set withfailbitclear still gives youoperator bool==trueβ she's fine, she's just at the end of the file πΈ
and that's the page! π huge shoutout to http://cppreference.com, version 2024.06.10, for keeping the lore alive β this one's dated 2024.06.10 too, so it's fresh. go write some while (stream >> x) loops and text me how it goes πβ¨π¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms