What is the logic behind those numbers? (1,4,2) |
As many flags, they increase by the power of two, to make possible to add (‘or’) them without overlapping other values.
In this case, you could for example use them this way:
std::cin.setstate(std::ios::badbit | std::ios::eofbit);
which would result in a state of 3. Since 3 is not a power of two, there can’t be another flag which has the value 3.
(Anyway, as jlb specified, the standard library doesn’t define them, so every compiler is implemented with its own set of data and paradoxically the developers could have chosen to set some values not as powers of two, which I really can’t believe.)
The usage is the same of the openmode flags
http://en.cppreference.com/w/cpp/io/ios_base/openmode
When you open a file, you can specify its open mode by those flags:
std::ofstream myfile(“mydata.txt”, std::ios_base::app | std::ios_base::binary);
Why might we need to use them? |
The standard library offers you some good alternatives, so you usually don’t need to use them. You can use:
if(cin.good()) // fast and comfortable
or
if(std::cin.rdstate() == std::ios::goodbit) // prolix
I was just giving you general examples, not suggesting any strategy.
is std::ios::failbit a static boolean public data member of the class istream? |
It’s more likely to be an integer rather than a boolean. The standard library says it is a “BitmaskType”, which:
Defines a type that can be used to represent a set of constant values or any combination of those values. This trait is typically implemented by integer types, std::bitset, or enumerations (scoped and unscoped) with additional operator overloads. |
http://en.cppreference.com/w/cpp/concept/BitmaskType
But it’s static, as you said, just read its documentation.