boostorg / boostorg/iostreams

Peeking into std::cin through a boost::iostreams::filtering_istream forever blocks

Open
#124 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
48
Forks
124
PR merge metrics
No merged PRs in 30d

Description

In the below code, I am wrapping `std::cin` into a `boost::iostreams::filtering_istream`. Calling `peek()` on the resulting object seems to block forever since I don't return from `peek()` whatever the number of characters that I type (pressing enter every now and then to flush).

```cpp
#include
#include

int main()
{
boost::iostreams::filtering_istream in;
in.push(std::cin); // tried in.push(boost::ref(std::cin))

if ('a' == std::cin.peek()) std::cout << "ok";
if ('a' == in.peek()) std::cout << "ok"; // blocks whatever the amount of characters typed, I expected it to behave like the std::cin.

return 0;
}
```

I could not find any indication in the [documentation](https://www.boost.org/doc/libs/1_75_0/libs/iostreams/doc/index.html) that suggests I am not using the library correctly. I tried to wrap `std::cin` with `boost::ref` when pushing it to the filtering stream but it did not change the behavior.

Interestingly, the problem only occurs when interactively typing to the standard input. If I provide data to the standard input through a pipeline, it works as expected:
```
echo "aaaa" | program
```

I tested the snippet with Visual Studio 2017 (boost 1.75.0 and 1.70.0) and gcc9.3 (boost 1.72.0) and none worked.

Thank you in advance for your help in figuring out what goes wrong.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.