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 (saystd::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?
std::string_view?std::streambufclass. It's not super complicated.std::istringstreamonly works withstd::string, hence its name. You need a customstd::streambufto work with a raw memory buffer. There are plenty of 3rd party implementations floating around if you don't want to write your own.