29 Input/output library [input.output]

29.8 String-based streams [string.streams]

29.8.1 Header <sstream> synopsis [sstream.syn]

namespace std {
  template<class charT, class traits = char_traits<charT>,
           class Allocator = allocator<charT>>
    class basic_stringbuf;

  using stringbuf  = basic_stringbuf<char>;
  using wstringbuf = basic_stringbuf<wchar_t>;

  template<class charT, class traits = char_traits<charT>,
           class Allocator = allocator<charT>>
    class basic_istringstream;

  using istringstream  = basic_istringstream<char>;
  using wistringstream = basic_istringstream<wchar_t>;

  template<class charT, class traits = char_traits<charT>,
           class Allocator = allocator<charT>>
    class basic_ostringstream;
  using ostringstream  = basic_ostringstream<char>;
  using wostringstream = basic_ostringstream<wchar_t>;

  template<class charT, class traits = char_traits<charT>,
           class Allocator = allocator<charT>>
    class basic_stringstream;
  using stringstream  = basic_stringstream<char>;
  using wstringstream = basic_stringstream<wchar_t>;
}
The header <sstream> defines four class templates and eight types that associate stream buffers with objects of class basic_­string, as described in [string.classes].

29.8.2 Class template basic_­stringbuf [stringbuf]

namespace std {
  template<class charT, class traits = char_traits<charT>,
           class Allocator = allocator<charT>>
  class basic_stringbuf : public basic_streambuf<charT, traits> {
  public:
    using char_type      = charT;
    using int_type       = typename traits::int_type;
    using pos_type       = typename traits::pos_type;
    using off_type       = typename traits::off_type;
    using traits_type    = traits;
    using allocator_type = Allocator;

    // [stringbuf.cons], constructors
    basic_stringbuf() : basic_stringbuf(ios_base::in | ios_base::out) {}
    explicit basic_stringbuf(ios_base::openmode which);
    explicit basic_stringbuf(
      const basic_string<charT, traits, Allocator>& s,
      ios_base::openmode which = ios_base::in | ios_base::out);
    explicit basic_stringbuf(const Allocator& a)
      : basic_stringbuf(ios_base::in | ios_base::out, a) {}
    basic_stringbuf(ios_base::openmode which, const Allocator& a);
    explicit basic_stringbuf(
      basic_string<charT, traits, Allocator>&& s,
      ios_base::openmode which = ios_base::in | ios_base::out);
    template<class SAlloc>
      basic_stringbuf(
        const basic_string<charT, traits, SAlloc>& s, const Allocator& a)
        : basic_stringbuf(s, ios_base::in | ios_base::out, a) {}
    template<class SAlloc>
      basic_stringbuf(
        const basic_string<charT, traits, SAlloc>& s,
        ios_base::openmode which, const Allocator& a);
    template<class SAlloc>
      explicit basic_stringbuf(
        const basic_string<charT, traits, SAlloc>& s,
        ios_base::openmode which = ios_base::in | ios_base::out);
    basic_stringbuf(const basic_stringbuf&) = delete;
    basic_stringbuf(basic_stringbuf&& rhs);
    basic_stringbuf(basic_stringbuf&& rhs, const Allocator& a);

    // [stringbuf.assign], assign and swap
    basic_stringbuf& operator=(const basic_stringbuf&) = delete;
    basic_stringbuf& operator=(basic_stringbuf&& rhs);
    void swap(basic_stringbuf& rhs) noexcept(see below);

    // [stringbuf.members], getters and setters
    allocator_type get_allocator() const noexcept;

    basic_string<charT, traits, Allocator> str() const &;
    template<class SAlloc>
      basic_string<charT,traits,SAlloc> str(const SAlloc& sa) const;
    basic_string<charT, traits, Allocator> str() &&;
    basic_string_view<charT, traits> view() const noexcept;

    void str(const basic_string<charT, traits, Allocator>& s);
    template<class SAlloc>
      void str(const basic_string<charT, traits, SAlloc>& s);
    void str(basic_string<charT, traits, Allocator>&& s);

  protected:
    // [stringbuf.virtuals], overridden virtual functions
    int_type underflow() override;
    int_type pbackfail(int_type c = traits::eof()) override;
    int_type overflow (int_type c = traits::eof()) override;
    basic_streambuf<charT, traits>* setbuf(charT*, streamsize) override;

    pos_type seekoff(off_type off, ios_base::seekdir way,
                     ios_base::openmode which
                      = ios_base::in | ios_base::out) override;
    pos_type seekpos(pos_type sp,
                     ios_base::openmode which
                      = ios_base::in | ios_base::out) override;

  private:
    ios_base::openmode mode;                        // exposition only
    basic_string<charT, traits, Allocator> buf;     // exposition only
    void init_buf_ptrs();                           // exposition only
  };

  template<class charT, class traits, class Allocator>
    void swap(basic_stringbuf<charT, traits, Allocator>& x,
              basic_stringbuf<charT, traits, Allocator>& y) noexcept(noexcept(x.swap(y)));
}
The class basic_­stringbuf is derived from basic_­streambuf to associate possibly the input sequence and possibly the output sequence with a sequence of arbitrary characters.
The sequence can be initialized from, or made available as, an object of class basic_­string.
For the sake of exposition, the maintained data and internal pointer initialization is presented here as:
  • ios_­base​::​openmode mode, has in set if the input sequence can be read, and out set if the output sequence can be written.
  • basic_­string<charT, traits, Allocator> buf contains the underlying character sequence.
  • init_­buf_­ptrs() sets the base class' get area ([streambuf.get.area]) and put area ([streambuf.put.area]) pointers after initializing, moving from, or assigning to buf accordingly.

29.8.2.1 Constructors [stringbuf.cons]

explicit basic_stringbuf(ios_base::openmode which);
Effects: Initializes the base class with basic_­streambuf() ([streambuf.cons]), and mode with which.
It is implementation-defined whether the sequence pointers (eback(), gptr(), egptr(), pbase(), pptr(), epptr()) are initialized to null pointers.
Postconditions: str().empty() is true.
explicit basic_stringbuf( const basic_string<charT, traits, Allocator>& s, ios_base::openmode which = ios_base::in | ios_base::out);
Effects: Initializes the base class with basic_­streambuf() ([streambuf.cons]), mode with which, and buf with s, then calls init_­buf_­ptrs().
basic_stringbuf(ios_base::openmode which, const Allocator &a);
Effects: Initializes the base class with basic_­streambuf() ([streambuf.cons]), mode with which, and buf with a, then calls init_­buf_­ptrs().
Postconditions: str().empty() is true.
explicit basic_stringbuf( basic_string<charT, traits, Allocator>&& s, ios_base::openmode which = ios_base::in | ios_base::out);
Effects: Initializes the base class with basic_­streambuf() ([streambuf.cons]), mode with which, and buf with std​::​move(s), then calls init_­buf_­ptrs().
template<class SAlloc> basic_stringbuf( const basic_string<charT, traits, SAlloc>& s, ios_base::openmode which, const Allocator &a);
Effects: Initializes the base class with basic_­streambuf() ([streambuf.cons]), mode with which, and buf with {s,a}, then calls init_­buf_­ptrs().
template<class SAlloc> explicit basic_stringbuf( const basic_string<charT, traits, SAlloc>& s, ios_base::openmode which = ios_base::in | ios_base::out);
Constraints: is_­same_­v<SAlloc,Allocator> is false.
Effects: Initializes the base class with basic_­streambuf() ([streambuf.cons]), mode with which, and buf with s, then calls init_­buf_­ptrs().
basic_stringbuf(basic_stringbuf&& rhs); basic_stringbuf(basic_stringbuf&& rhs, const Allocator& a);
Effects: Copy constructs the base class from rhs and initializes mode with rhs.mode.
In the first form buf is initialized from std​::​move(rhs).str().
In the second form buf is initialized from {std​::​move(rhs).str(), a}.
It is implementation-defined whether the sequence pointers in *this (eback(), gptr(), egptr(), pbase(), pptr(), epptr()) obtain the values which rhs had.
Postconditions: Let rhs_­p refer to the state of rhs just prior to this construction and let rhs_­a refer to the state of rhs just after this construction.
  • str() == rhs_­p.str()
  • gptr() - eback() == rhs_­p.gptr() - rhs_­p.eback()
  • egptr() - eback() == rhs_­p.egptr() - rhs_­p.eback()
  • pptr() - pbase() == rhs_­p.pptr() - rhs_­p.pbase()
  • epptr() - pbase() == rhs_­p.epptr() - rhs_­p.pbase()
  • if (eback()) eback() != rhs_­a.eback()
  • if (gptr()) gptr() != rhs_­a.gptr()
  • if (egptr()) egptr() != rhs_­a.egptr()
  • if (pbase()) pbase() != rhs_­a.pbase()
  • if (pptr()) pptr() != rhs_­a.pptr()
  • if (epptr()) epptr() != rhs_­a.epptr()
  • getloc() == rhs_­p.getloc()
  • rhs is empty but usable, as if std​::​move(rhs).str() was called.

29.8.2.2 Assignment and swap [stringbuf.assign]

basic_stringbuf& operator=(basic_stringbuf&& rhs);
Effects: After the move assignment *this has the observable state it would have had if it had been move constructed from rhs (see [stringbuf.cons]).
Returns: *this.
void swap(basic_stringbuf& rhs) noexcept(see below);
Preconditions: allocator_­traits<Allocator>​::​propagate_­on_­container_­swap​::​value is true or get_­allocator() == s.get_­allocator() is true.
Effects: Exchanges the state of *this and rhs.
Remarks: The expression inside noexcept is equivalent to:
allocator_­traits<Allocator>​::​propagate_­on_­container_­swap​::​value ||
allocator_­traits<Allocator>​::​is_­always_­equal​::​value.
template<class charT, class traits, class Allocator> void swap(basic_stringbuf<charT, traits, Allocator>& x, basic_stringbuf<charT, traits, Allocator>& y) noexcept(noexcept(x.swap(y)));
Effects: Equivalent to: x.swap(y).

29.8.2.3 Member functions [stringbuf.members]

The member functions getting the underlying character sequence all refer to a high_­mark value, where high_­mark represents the position one past the highest initialized character in the buffer.
Characters can be initialized by writing to the stream, by constructing the basic_­stringbuf passing a basic_­string argument, or by calling one of the str member functions passing a basic_­string as an argument.
In the latter case, all characters initialized prior to the call are now considered uninitialized (except for those characters re-initialized by the new basic_­string).
void init_buf_ptrs(); // exposition only
Effects: Initializes the input and output sequences from buf according to mode.
Postconditions:
  • If ios_­base​::​out is set in mode, pbase() points to buf.front() and epptr() >= pbase() + buf.size() is true;
    • in addition, if ios_­base​::​ate is set in mode, pptr() == pbase() + buf.size() is true,
    • otherwise pptr() == pbase() is true.
  • If ios_­base​::​in is set in mode, eback() points to buf.front(), and (gptr() == eback() && egptr() == eback() + buf.size()) is true.
Note
:
For efficiency reasons, stream buffer operations might violate invariants of buf while it is held encapsulated in the basic_­stringbuf, e.g., by writing to characters in the range [buf.data() + buf.size(), buf.data() + buf.capacity()).
All operations retrieving a basic_­string from buf ensure that the basic_­string invariants hold on the returned value.
— end note
 ]
allocator_type get_allocator() const noexcept;
Returns: buf.get_­allocator().
basic_string<charT, traits, Allocator> str() const &;
Effects: Equivalent to:
return basic_string<charT, traits, Allocator>(view(), get_allocator());
template<class SAlloc> basic_string<charT, traits, SAlloc> str(const SAlloc& sa) const;
Constraints: SAlloc is a type that qualifies as an allocator ([container.requirements.general]).
Effects: Equivalent to:
return basic_string<charT, traits, SAlloc>(view(), sa);
basic_string<charT, traits, Allocator> str() &&;
Returns: A basic_­string<charT, traits, Allocator> object move constructed from the basic_­stringbuf's underlying character sequence in buf.
This can be achieved by first adjusting buf to have the same content as view().
Postconditions: The underlying character sequence buf is empty and pbase(), pptr(), epptr(), eback(), gptr(), and egptr() are initialized as if by calling init_­buf_­ptrs() with an empty buf.
basic_string_view<charT, traits> view() const noexcept;
Let sv be basic_­string_­view<charT, traits>.
Returns: A sv object referring to the basic_­stringbuf's underlying character sequence in buf:
  • If ios_­base​::​out is set in mode, then sv(pbase(), high_­mark-pbase()) is returned.
  • Otherwise, if ios_­base​::​in is set in mode, then sv(eback(), egptr()-eback()) is returned.
  • Otherwise, sv() is returned.
Note
:
Using the returned sv object after destruction or invalidation of the character sequence underlying *this is undefined behavior, unless sv.empty() is true.
— end note
 ]
void str(const basic_string<charT, traits, Allocator>& s);
Effects: Equivalent to:
buf = s;
init_buf_ptrs();
template<class SAlloc> void str(const basic_string<charT, traits, SAlloc>& s);
Constraints: is_­same_­v<SAlloc,Allocator> is false.
Effects: Equivalent to:
buf = s;
init_buf_ptrs();
void str(basic_string<charT, traits, Allocator>&& s);
Effects: Equivalent to:
buf = std::move(s);
init_buf_ptrs();

29.8.2.4 Overridden virtual functions [stringbuf.virtuals]

int_type underflow() override;
Returns: If the input sequence has a read position available, returns traits​::​to_­int_­type(*gptr()).
Otherwise, returns traits​::​eof().
Any character in the underlying buffer which has been initialized is considered to be part of the input sequence.
int_type pbackfail(int_type c = traits::eof()) override;
Effects: Puts back the character designated by c to the input sequence, if possible, in one of three ways:
  • If traits​::​eq_­int_­type(c, traits​::​eof()) returns false and if the input sequence has a putback position available, and if traits​::​eq(to_­char_­type(c), gptr()[-1]) returns true, assigns gptr() - 1 to gptr().
    Returns: c.
  • If traits​::​eq_­int_­type(c, traits​::​eof()) returns false and if the input sequence has a putback position available, and if mode & ios_­base​::​out is nonzero, assigns c to *--gptr().
    Returns: c.
  • If traits​::​eq_­int_­type(c, traits​::​eof()) returns true and if the input sequence has a putback position available, assigns gptr() - 1 to gptr().
    Returns: traits​::​not_­eof(c).
Returns: As specified above, or traits​::​eof() to indicate failure.
Remarks: If the function can succeed in more than one of these ways, it is unspecified which way is chosen.
int_type overflow(int_type c = traits::eof()) override;
Effects: Appends the character designated by c to the output sequence, if possible, in one of two ways:
  • If traits​::​eq_­int_­type(c, traits​::​eof()) returns false and if either the output sequence has a write position available or the function makes a write position available (as described below), the function calls sputc(c).
    Signals success by returning c.
  • If traits​::​eq_­int_­type(c, traits​::​eof()) returns true, there is no character to append.
    Signals success by returning a value other than traits​::​eof().
Remarks: The function can alter the number of write positions available as a result of any call.
Returns: As specified above, or traits​::​eof() to indicate failure.
The function can make a write position available only if ios_­base​::​out is set in mode.
To make a write position available, the function reallocates (or initially allocates) an array object with a sufficient number of elements to hold the current array object (if any), plus at least one additional write position.
If ios_­base​::​in is set in mode, the function alters the read end pointer egptr() to point just past the new write position.
pos_type seekoff(off_type off, ios_base::seekdir way, ios_base::openmode which = ios_base::in | ios_base::out) override;
Effects: Alters the stream position within one of the controlled sequences, if possible, as indicated in Table 124.
Table 124: seekoff positioning   [tab:stringbuf.seekoff.pos]
Conditions
Result
ios_­base​::​in is set in which
positions the input sequence
ios_­base​::​out is set in which
positions the output sequence
both ios_­base​::​in and ios_­base​::​out are set in which and either
way == ios_­base​::​beg or
way == ios_­base​::​end
positions both the input and the output sequences
Otherwise
the positioning operation fails.
For a sequence to be positioned, the function determines newoff as indicated in Table 125.
If the sequence's next pointer (either gptr() or pptr()) is a null pointer and newoff is nonzero, the positioning operation fails.
Table 125: newoff values   [tab:stringbuf.seekoff.newoff]
Condition
newoff Value
way == ios_­base​::​beg
0
way == ios_­base​::​cur
the next pointer minus the beginning pointer (xnext - xbeg).
way == ios_­base​::​end
the high mark pointer minus the beginning pointer (high_­mark - xbeg).
If (newoff + off) < 0, or if newoff + off refers to an uninitialized character ([stringbuf.members]), the positioning operation fails.
Otherwise, the function assigns xbeg + newoff + off to the next pointer xnext.
Returns: pos_­type(newoff), constructed from the resultant offset newoff (of type off_­type), that stores the resultant stream position, if possible.
If the positioning operation fails, or if the constructed object cannot represent the resultant stream position, the return value is pos_­type(off_­type(-1)).
pos_type seekpos(pos_type sp, ios_base::openmode which = ios_base::in | ios_base::out) override;
Effects: Equivalent to seekoff(off_­type(sp), ios_­base​::​beg, which).
Returns: sp to indicate success, or pos_­type(off_­type(-1)) to indicate failure.
basic_streambuf<charT, traits>* setbuf(charT* s, streamsize n);
Effects: implementation-defined, except that setbuf(0, 0) has no effect.
Returns: this.

29.8.3 Class template basic_­istringstream [istringstream]

namespace std {
  template<class charT, class traits = char_traits<charT>,
           class Allocator = allocator<charT>>
  class basic_istringstream : public basic_istream<charT, traits> {
  public:
    using char_type      = charT;
    using int_type       = typename traits::int_type;
    using pos_type       = typename traits::pos_type;
    using off_type       = typename traits::off_type;
    using traits_type    = traits;
    using allocator_type = Allocator;

    // [istringstream.cons], constructors
    basic_istringstream() : basic_istringstream(ios_base::in) {}
    explicit basic_istringstream(ios_base::openmode which);
    explicit basic_istringstream(
      const basic_string<charT, traits, Allocator>& s,
      ios_base::openmode which = ios_base::in);
    basic_istringstream(ios_base::openmode which, const Allocator& a);
    explicit basic_istringstream(
      basic_string<charT, traits, Allocator>&& s,
      ios_base::openmode which = ios_base::in);
    template<class SAlloc>
      basic_istringstream(
        const basic_string<charT, traits, SAlloc>& s, const Allocator& a)
        : basic_istringstream(s, ios_base::in, a) {}
    template<class SAlloc>
      basic_istringstream(
        const basic_string<charT, traits, SAlloc>& s,
        ios_base::openmode which, const Allocator& a);
    template<class SAlloc>
      explicit basic_istringstream(
        const basic_string<charT, traits, SAlloc>& s,
        ios_base::openmode which = ios_base::in);
    basic_istringstream(const basic_istringstream&) = delete;
    basic_istringstream(basic_istringstream&& rhs);

    // [istringstream.assign], assign and swap
    basic_istringstream& operator=(const basic_istringstream&) = delete;
    basic_istringstream& operator=(basic_istringstream&& rhs);
    void swap(basic_istringstream& rhs);

    // [istringstream.members], members
    basic_stringbuf<charT, traits, Allocator>* rdbuf() const;
    basic_string<charT, traits, Allocator> str() const &;
    template<class SAlloc>
      basic_string<charT,traits,SAlloc> str(const SAlloc& sa) const;
    basic_string<charT, traits, Allocator> str() &&;
    basic_string_view<charT, traits> view() const noexcept;

    void str(const basic_string<charT, traits, Allocator>& s);
    template<class SAlloc>
      void str(const basic_string<charT, traits, SAlloc>& s);
    void str(basic_string<charT, traits, Allocator>&& s);

  private:
    basic_stringbuf<charT, traits, Allocator> sb;   // exposition only
  };

  template<class charT, class traits, class Allocator>
    void swap(basic_istringstream<charT, traits, Allocator>& x,
              basic_istringstream<charT, traits, Allocator>& y);
}
The class basic_­istringstream<charT, traits, Allocator> supports reading objects of class basic_­string<​charT, traits, Allocator>.
It uses a basic_­stringbuf<charT, traits, Allocator> object to control the associated storage.
For the sake of exposition, the maintained data is presented here as:
  • sb, the stringbuf object.

29.8.3.1 Constructors [istringstream.cons]

explicit basic_istringstream(ios_base::openmode which);
Effects: Initializes the base class with basic_­istream<charT, traits>(addressof(sb)) ([istream]) and sb with basic_­stringbuf<charT, traits, Allocator>(which | ios_­base​::​in) ([stringbuf.cons]).
explicit basic_istringstream( const basic_string<charT, traits, Allocator>& s, ios_base::openmode which = ios_base::in);
Effects: Initializes the base class with basic_­istream<charT, traits>(addressof(sb)) ([istream]) and sb with basic_­stringbuf<charT, traits, Allocator>(s, which | ios_­base​::​in)
([stringbuf.cons]).
basic_istringstream(ios_base::openmode which, const Allocator& a);
Effects: Initializes the base class with basic_­istream<charT, traits>(addressof(sb)) ([istream]) and sb with basic_­stringbuf<charT, traits, Allocator>(which | ios_­base​::​in, a) ([stringbuf.cons]).
explicit basic_istringstream( basic_string<charT, traits, Allocator>&& s, ios_base::openmode which = ios_base::in);
Effects: Initializes the base class with basic_­istream<charT, traits>(addressof(sb)) ([istream]) and sb with basic_­stringbuf<charT, traits, Allocator>(std​::​move(s), which | ios_­base​::​​in) ([stringbuf.cons]).
template<class SAlloc> basic_istringstream( const basic_string<charT, traits, SAlloc>& s, ios_base::openmode which, const Allocator& a);
Effects: Initializes the base class with basic_­istream<charT, traits>(addressof(sb)) ([istream]) and sb with basic_­stringbuf<charT, traits, Allocator>(s, which | ios_­base​::​in, a)
([stringbuf.cons]).
template<class SAlloc> explicit basic_istringstream( const basic_string<charT, traits, SAlloc>& s, ios_base::openmode which = ios_base::in);
Effects: Initializes the base class with basic_­istream<charT, traits>(addressof(sb)) ([istream]) and sb with basic_­stringbuf<charT, traits, Allocator>(s, which | ios_­base​::​in) ([stringbuf.cons]).
basic_istringstream(basic_istringstream&& rhs);
Effects: Move constructs from the rvalue rhs.
This is accomplished by move constructing the base class, and the contained basic_­stringbuf.
Then calls basic_­istream<charT, traits>​::​set_­rdbuf(addressof(sb)) to install the contained basic_­stringbuf.

29.8.3.2 Assignment and swap [istringstream.assign]

void swap(basic_istringstream& rhs);
Effects: Equivalent to:
basic_istream<charT, traits>::swap(rhs);
sb.swap(rhs.sb);
template<class charT, class traits, class Allocator> void swap(basic_istringstream<charT, traits, Allocator>& x, basic_istringstream<charT, traits, Allocator>& y);
Effects: Equivalent to: x.swap(y).

29.8.3.3 Member functions [istringstream.members]

basic_stringbuf<charT, traits, Allocator>* rdbuf() const;
Returns: const_­cast<basic_­stringbuf<charT, traits, Allocator>*>(addressof(sb)).
basic_string<charT, traits, Allocator> str() const &;
Effects: Equivalent to: return rdbuf()->str();
template<class SAlloc> basic_string<charT,traits,SAlloc> str(const SAlloc& sa) const;
Effects: Equivalent to: return rdbuf()->str(sa);
basic_string<charT,traits,Allocator> str() &&;
Effects: Equivalent to: return std​::​move(*rdbuf()).str();
basic_string_view<charT, traits> view() const noexcept;
Effects: Equivalent to: return rdbuf()->view();
void str(const basic_string<charT, traits, Allocator>& s);
Effects: Equivalent to: rdbuf()->str(s);
template<class SAlloc> void str(const basic_string<charT, traits, SAlloc>& s);
Effects: Equivalent to: rdbuf()->str(s);
void str(basic_string<charT, traits, Allocator>&& s);
Effects: Equivalent to: rdbuf()->str(std​::​move(s));

29.8.4 Class template basic_­ostringstream [ostringstream]

namespace std {
  template<class charT, class traits = char_traits<charT>,
           class Allocator = allocator<charT>>
  class basic_ostringstream : public basic_ostream<charT, traits> {
  public:
    using char_type      = charT;
    using int_type       = typename traits::int_type;
    using pos_type       = typename traits::pos_type;
    using off_type       = typename traits::off_type;
    using traits_type    = traits;
    using allocator_type = Allocator;

    // [ostringstream.cons], constructors
    basic_ostringstream() : basic_ostringstream(ios_base::out) {}
    explicit basic_ostringstream(ios_base::openmode which);
    explicit basic_ostringstream(
      const basic_string<charT, traits, Allocator>& s,
      ios_base::openmode which = ios_base::out);
    basic_ostringstream(ios_base::openmode which, const Allocator& a);
    explicit basic_ostringstream(
      basic_string<charT, traits, Allocator>&& s,
      ios_base::openmode which = ios_base::out);
    template<class SAlloc>
      basic_ostringstream(
        const basic_string<charT, traits, SAlloc>& s, const Allocator& a)
        : basic_ostringstream(s, ios_base::out, a) {}
    template<class SAlloc>
      basic_ostringstream(
        const basic_string<charT, traits, SAlloc>& s,
        ios_base::openmode which, const Allocator& a);
    template<class SAlloc>
      explicit basic_ostringstream(
        const basic_string<charT, traits, SAlloc>& s,
        ios_base::openmode which = ios_base::out);
    basic_ostringstream(const basic_ostringstream&) = delete;
    basic_ostringstream(basic_ostringstream&& rhs);

    // [ostringstream.assign], assign and swap
    basic_ostringstream& operator=(const basic_ostringstream&) = delete;
    basic_ostringstream& operator=(basic_ostringstream&& rhs);
    void swap(basic_ostringstream& rhs);

    // [ostringstream.members], members
    basic_stringbuf<charT, traits, Allocator>* rdbuf() const;

    basic_string<charT, traits, Allocator> str() const &;
    template<class SAlloc>
      basic_string<charT,traits,SAlloc> str(const SAlloc& sa) const;
    basic_string<charT, traits, Allocator> str() &&;
    basic_string_view<charT, traits> view() const noexcept;

    void str(const basic_string<charT, traits, Allocator>& s);
    template<class SAlloc>
      void str(const basic_string<charT, traits, SAlloc>& s);
    void str(basic_string<charT, traits, Allocator>&& s);

   private:
    basic_stringbuf<charT, traits, Allocator> sb;   // exposition only
  };

  template<class charT, class traits, class Allocator>
    void swap(basic_ostringstream<charT, traits, Allocator>& x,
              basic_ostringstream<charT, traits, Allocator>& y);
}
The class basic_­ostringstream<charT, traits, Allocator> supports writing objects of class basic_­string<​charT, traits, Allocator>.
It uses a basic_­stringbuf object to control the associated storage.
For the sake of exposition, the maintained data is presented here as:
  • sb, the stringbuf object.

29.8.4.1 Constructors [ostringstream.cons]

explicit basic_ostringstream(ios_base::openmode which);
Effects: Initializes the base class with basic_­ostream<charT, traits>(addressof(sb)) ([ostream]) and sb with basic_­stringbuf<charT, traits, Allocator>(which | ios_­base​::​out) ([stringbuf.cons]).
explicit basic_ostringstream( const basic_string<charT, traits, Allocator>& s, ios_base::openmode which = ios_base::out);
Effects: Initializes the base class with basic_­ostream<charT, traits>(addressof(sb)) ([ostream]) and sb with basic_­stringbuf<charT, traits, Allocator>(s, which | ios_­base​::​out)
([stringbuf.cons]).
basic_ostringstream(ios_base::openmode which, const Allocator& a);
Effects: Initializes the base class with basic_­ostream<charT, traits>(addressof(sb)) ([ostream]) and sb with basic_­stringbuf<charT, traits, Allocator>(which | ios_­base​::​out, a)
([stringbuf.cons]).
explicit basic_ostringstream( basic_string<charT, traits, Allocator>&& s, ios_base::openmode which = ios_base::out);
Effects: Initializes the base class with basic_­ostream<charT, traits>(addressof(sb)) ([ostream]) and sb with basic_­stringbuf<charT, traits, Allocator>(std​::​move(s), which | ios_­base​::​​out) ([stringbuf.cons]).
template<class SAlloc> basic_ostringstream( const basic_string<charT, traits, SAlloc>& s, ios_base::openmode which, const Allocator& a);
Effects: Initializes the base class with basic_­ostream<charT, traits>(addressof(sb)) ([ostream]) and sb with basic_­stringbuf<charT, traits, Allocator>(s, which | ios_­base​::​out, a)
([stringbuf.cons]).
template<class SAlloc> explicit basic_ostringstream( const basic_string<charT, traits, SAlloc>& s, ios_base::openmode which = ios_base::out);
Constraints: is_­same_­v<SAlloc,Allocator> is false.
Effects: Initializes the base class with basic_­ostream<charT, traits>(addressof(sb)) ([ostream]) and sb with basic_­stringbuf<charT, traits, Allocator>(s, which | ios_­base​::​out)
([stringbuf.cons]).
basic_ostringstream(basic_ostringstream&& rhs);
Effects: Move constructs from the rvalue rhs.
This is accomplished by move constructing the base class, and the contained basic_­stringbuf.
Then calls basic_­ostream<charT, traits>​::​set_­rdbuf(addressof(sb)) to install the contained basic_­stringbuf.

29.8.4.2 Assignment and swap [ostringstream.assign]

void swap(basic_ostringstream& rhs);
Effects: Equivalent to:
basic_ostream<charT, traits>::swap(rhs);
sb.swap(rhs.sb);
template<class charT, class traits, class Allocator> void swap(basic_ostringstream<charT, traits, Allocator>& x, basic_ostringstream<charT, traits, Allocator>& y);
Effects: Equivalent to: x.swap(y).

29.8.4.3 Member functions [ostringstream.members]

basic_stringbuf<charT, traits, Allocator>* rdbuf() const;
Returns: const_­cast<basic_­stringbuf<charT, traits, Allocator>*>(addressof(sb)).
basic_string<charT, traits, Allocator> str() const &;
Effects: Equivalent to: return rdbuf()->str();
template<class SAlloc> basic_string<charT,traits,SAlloc> str(const SAlloc& sa) const;
Effects: Equivalent to: return rdbuf()->str(sa);
basic_string<charT,traits,Allocator> str() &&;
Effects: Equivalent to: return std​::​move(*rdbuf()).str();
basic_string_view<charT, traits> view() const noexcept;
Effects: Equivalent to: return rdbuf()->view();
void str(const basic_string<charT, traits, Allocator>& s);
Effects: Equivalent to: rdbuf()->str(s);
template<class SAlloc> void str(const basic_string<charT, traits, SAlloc>& s);
Effects: Equivalent to: rdbuf()->str(s);
void str(basic_string<charT, traits, Allocator>&& s);
Effects: Equivalent to: rdbuf()->str(std​::​move(s));

29.8.5 Class template basic_­stringstream [stringstream]

namespace std {
  template<class charT, class traits = char_traits<charT>,
           class Allocator = allocator<charT>>
  class basic_stringstream : public basic_iostream<charT, traits> {
  public:
    using char_type      = charT;
    using int_type       = typename traits::int_type;
    using pos_type       = typename traits::pos_type;
    using off_type       = typename traits::off_type;
    using traits_type    = traits;
    using allocator_type = Allocator;

    // [stringstream.cons], constructors
    basic_stringstream() : basic_stringstream(ios_base::out | ios_base::in) {}
    explicit basic_stringstream(ios_base::openmode which);
    explicit basic_stringstream(
      const basic_string<charT, traits, Allocator>& s,
      ios_base::openmode which = ios_base::out | ios_base::in);
    basic_stringstream(ios_base::openmode which, const Allocator& a);
    explicit basic_stringstream(
      basic_string<charT, traits, Allocator>&& s,
      ios_base::openmode which = ios_base::out | ios_base::in);
    template<class SAlloc>
      basic_stringstream(
        const basic_string<charT, traits, SAlloc>& s, const Allocator& a)
        : basic_stringstream(s, ios_base::out | ios_base::in, a) {}
    template<class SAlloc>
      basic_stringstream(
        const basic_string<charT, traits, SAlloc>& s,
        ios_base::openmode which, const Allocator& a);
    template<class SAlloc>
      explicit basic_stringstream(
        const basic_string<charT, traits, SAlloc>& s,
        ios_base::openmode which = ios_base::out | ios_base::in);
    basic_stringstream(const basic_stringstream&) = delete;
    basic_stringstream(basic_stringstream&& rhs);

    // [stringstream.assign], assign and swap
    basic_stringstream& operator=(const basic_stringstream&) = delete;
    basic_stringstream& operator=(basic_stringstream&& rhs);
    void swap(basic_stringstream& rhs);

    // [stringstream.members], members
    basic_stringbuf<charT, traits, Allocator>* rdbuf() const;

    basic_string<charT, traits, Allocator> str() const &;
    template<class SAlloc>
      basic_string<charT,traits,SAlloc> str(const SAlloc& sa) const;
    basic_string<charT, traits, Allocator> str() &&;
    basic_string_view<charT, traits> view() const noexcept;

    void str(const basic_string<charT, traits, Allocator>& s);
    template<class SAlloc>
      void str(const basic_string<charT, traits, SAlloc>& s);
    void str(basic_string<charT, traits, Allocator>&& s);

  private:
    basic_stringbuf<charT, traits> sb;  // exposition only
  };

  template<class charT, class traits, class Allocator>
    void swap(basic_stringstream<charT, traits, Allocator>& x,
              basic_stringstream<charT, traits, Allocator>& y);
}
The class template basic_­stringstream<charT, traits> supports reading and writing from objects of class basic_­string<charT, traits, Allocator>.
It uses a basic_­stringbuf<charT, traits, Allocator> object to control the associated sequence.
For the sake of exposition, the maintained data is presented here as
  • sb, the stringbuf object.

29.8.5.1 Constructors [stringstream.cons]

explicit basic_stringstream(ios_base::openmode which);
Effects: Initializes the base class with basic_­iostream<charT, traits>(addressof(sb)) ([iostream.cons]) and sb with basic_­stringbuf<charT, traits, Allocator>(which).
explicit basic_stringstream( const basic_string<charT, traits, Allocator>& s, ios_base::openmode which = ios_base::out | ios_base::in);
Effects: Initializes the base class with basic_­iostream<charT, traits>(addressof(sb)) ([iostream.cons]) and sb with basic_­stringbuf<charT, traits, Allocator>(s, which).
basic_stringstream(ios_base::openmode which, const Allocator& a);
Effects: Initializes the base class with basic_­iostream<charT, traits>(addressof(sb)) ([iostream.cons]) and sb with basic_­stringbuf<charT, traits, Allocator>(which, a) ([stringbuf.cons]).
explicit basic_stringstream( basic_string<charT, traits, Allocator>&& s, ios_base::openmode which = ios_base::out | ios_base::in);
Effects: Initializes the base class with basic_­iostream<charT, traits>(addressof(sb)) ([iostream.cons]) and sb with basic_­stringbuf<charT, traits, Allocator>(std​::​move(s), which) ([stringbuf.cons]).
template<class SAlloc> basic_stringstream( const basic_string<charT, traits, SAlloc>& s, ios_base::openmode which, const Allocator& a);
Effects: Initializes the base class with basic_­iostream<charT, traits>(addressof(sb)) ([iostream.cons]) and sb with basic_­stringbuf<charT, traits, Allocator>(s, which, a) ([stringbuf.cons]).
template<class SAlloc> explicit basic_stringstream( const basic_string<charT, traits, SAlloc>& s, ios_base::openmode which = ios_base::out | ios_base::in);
Constraints: is_­same_­v<SAlloc,Allocator> is false.
Effects: Initializes the base class with basic_­iostream<charT, traits>(addressof(sb)) ([iostream.cons]) and sb with basic_­stringbuf<charT, traits, Allocator>(s, which) ([stringbuf.cons]).
basic_stringstream(basic_stringstream&& rhs);
Effects: Move constructs from the rvalue rhs.
This is accomplished by move constructing the base class, and the contained basic_­stringbuf.
Then calls basic_­istream<charT, traits>​::​set_­rdbuf(addressof(sb)) to install the contained basic_­stringbuf.

29.8.5.2 Assignment and swap [stringstream.assign]

void swap(basic_stringstream& rhs);
Effects: Equivalent to:
basic_iostream<charT,traits>::swap(rhs);
sb.swap(rhs.sb);
template<class charT, class traits, class Allocator> void swap(basic_stringstream<charT, traits, Allocator>& x, basic_stringstream<charT, traits, Allocator>& y);
Effects: Equivalent to: x.swap(y).

29.8.5.3 Member functions [stringstream.members]

basic_stringbuf<charT, traits, Allocator>* rdbuf() const;
Returns: const_­cast<basic_­stringbuf<charT, traits, Allocator>*>(addressof(sb)).
basic_string<charT, traits, Allocator> str() const &;
Effects: Equivalent to: return rdbuf()->str();
template<class SAlloc> basic_string<charT,traits,SAlloc> str(const SAlloc& sa) const;
Effects: Equivalent to: return rdbuf()->str(sa);
basic_string<charT,traits,Allocator> str() &&;
Effects: Equivalent to: return std​::​move(*rdbuf()).str();
basic_string_view<charT, traits> view() const noexcept;
Effects: Equivalent to: return rdbuf()->view();
void str(const basic_string<charT, traits, Allocator>& s);
Effects: Equivalent to: rdbuf()->str(s);
template<class SAlloc> void str(const basic_string<charT, traits, SAlloc>& s);
Effects: Equivalent to: rdbuf()->str(s);
void str(basic_string<charT, traits, Allocator>&& s);
Effects: Equivalent to: rdbuf()->str(std​::​move(s));