0

Suppose I have two APIs that I need to interact with:

  • API 1 is a producer which provides me with a raw character buffer (const char*) and a length (say std::size_t).
  • API 2 is a consumer which expects an std::istream.

I'd like to use this buffer for a std::istringstream, but if I'm not mistaken, I'll have to turn the raw character buffer into a std::string first, copying its contents. I'd like to avoid that.

So my question is: Is there a way I could directly make an istringstream read from a buffer without copying the buffer using the stdlib, and without writing my own bufferstream class?

Is there a good reason for istringstreams being restricted to, well, strings - especially since using strings as buffers is considered bad practice?

6
  • Maybe: stackoverflow.com/questions/26418693/… Commented Jan 16, 2024 at 20:28
  • @NathanOliver that's almost what I want, but I would not want to rely on implementation-defined behavior, if possible. Commented Jan 16, 2024 at 20:30
  • Write a input stream compatible wrapper around std::string_view? Commented Jan 16, 2024 at 20:32
  • 3
    @Luatic: Then make your own std::streambuf class. It's not super complicated. Commented Jan 16, 2024 at 20:34
  • std::istringstream only works with std::string, hence its name. You need a custom std::streambuf to work with a raw memory buffer. There are plenty of 3rd party implementations floating around if you don't want to write your own. Commented Jan 16, 2024 at 20:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.