Search code examples
c++strstreamc++23

How will std::spanstream usually be used in C++?


<spanstream> will debut in C++23 (see cppreference). According to the proposal, they are string-streams with std::span based buffers.

My questions are:

  • Does std::spanstream have somewhat equivalent uses of the old std::strstream (or strstream deprecated in C++ 98)?
  • What will be the benefits of using them after the full release of C++ 23?

Solution

  • They are intended to be a near drop-in replacement for strstream (except with proper bounds checking). As such, they will have the exact same use cases. When you have an existing buffer that you want to stream into/outof.

    The ability to move a std::string into stringstreams added in C++20 eliminated the use case when the existing buffer is in a std::string. But sometimes you just have a naked char const* with a known length.