Yep. stringstreams make copies of passed strings too. I had to hack around that once like this:
struct ZeroCopyBuf : public std::streambuf
{
ZeroCopyBuf(const std::string &s) : ZeroCopyBuf(s.c_str(), s.length()) {}
ZeroCopyBuf(const char *c, std::size_t l) : ZeroCopyBuf(const_cast<char*>(c), l) {}
ZeroCopyBuf(char *c, std::size_t l) { setg(c, c, c + l); }
};
...
std::string s ...;
ZeroCopyBuf buf(s);
std::istream is(&buf);
Terrible.
C++-20 makes it better by adding move semantics to most methods... Still.