Why does in_avail() output zero even if the stream has some char?

c++

Solution

What must have happened is that your `putback` didn't find any room in the `streambuf` get area associated with `std::cin` (otherwise a read position would have been available and `egptr() - gptr()` would have been non-zero) and must have gone to an underlying layer thanks to `pbackfail`.

`in_avail()` will call `showmanyc()` and zero (which is the default implementation of this virtual function) is a safe thing to return as it means that a read might block and it might fail but isn't guaranteed to do either. Obviously it is possible for an implementation to provide a more helpful implementation for `showmanyc()` in this case, but the simple implementation is cheap and conformant.

Problem

``` #include <iostream> int main( ) { using namespace std; cout << cin.rdbuf()->in_avail() << endl; cin.putback(1); cin.putback(1); cout << cin.rdbuf()->in_avail() << endl; return 0; } //compile by g++-4.8.1 ``` I think this will output 0 and 2 but when I run the code, it output 0 and 0, why? or if I change cin.putback(1); to int a; cin >> a; with input 12 12; it still outputs 0 and 0

Original source

Related problems