# std::basic_ios<CharT,Traits>::operator bool

> C++ Atlas: `cppatlas.com/w/cpp/io/basic_ios/operator_bool` | Source: https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool | Domain: Input/output | Standards: C++11

Checks whether the stream has no errors.

## Official Specification Summary (cppreference)

```cpp
operator /* unspecified-boolean-type */() const; // (1) (until C++11)
explicit operator bool() const; // (2) (since C++11)
```

Checks whether the stream has no errors.

1) Returns a value that evaluates to `false` in a boolean context if `fail()` returns `true`, otherwise returns a value that evaluates to `true` in a boolean context.

2) Returns `true` if 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, resulting in the idiomatic C++ input loops such as `while (stream >> value) {...}` or `while (std::getline(stream, string)) {...}`. Such loops execute the loop's body only if the input operation succeeded.

### Parameters

(none)

### Return value

1) A value that evaluates to `true` in a boolean context if the stream has no errors, a value that evaluates to `false` in a boolean context otherwise.

2) `true` if the stream has no errors, `false` otherwise.

### 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.

In C++98, `operator bool` could not be provided directly due to the safe bool problem. The initial solution in C++98 is to provide `operator void*`, which returns a null pointer if `fail()` returns `true` or 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 be `explicit`. The resolution of LWG issue 1094 introduced the explicit `operator bool` and the boolean conversion is now safe.

### Example

```cpp
#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:

```text
s is true
1
2
3
s is false
```

### Defect reports

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 | `operator void*` was provided | a conversion function to an unspecified boolean type is provided instead |

### See also

The following table shows the value of [`basic_ios`](https://en.cppreference.com/w/cpp/io/basic_ios) accessors ([`good()`](https://en.cppreference.com/w/cpp/io/basic_ios/good), [`fail()`](https://en.cppreference.com/w/cpp/io/basic_ios/fail), etc.) for all possible combinations of [`ios_base::iostate`](https://en.cppreference.com/w/cpp/io/ios_base/iostate) flags:

|   |   |   |   |   |   |   |   |   |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| [`ios_base::iostate`](https://en.cppreference.com/w/cpp/io/ios_base/iostate) flags |   |   | `basic_ios` accessors |   |   |   |   |   |
| `eofbit` | `failbit` | `badbit` | [`good()`](https://en.cppreference.com/w/cpp/io/basic_ios/good) | [`fail()`](https://en.cppreference.com/w/cpp/io/basic_ios/fail) | [`bad()`](https://en.cppreference.com/w/cpp/io/basic_ios/bad) | [`eof()`](https://en.cppreference.com/w/cpp/io/basic_ios/eof) | `operator bool` | [`operator!`](https://en.cppreference.com/w/cpp/io/basic_ios/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 |

---
Attribution: content from https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.