29 Input/output library [input.output]

29.8 String-based streams [string.streams]

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.
Next basic_­ostream<charT, traits>​::​set_­rdbuf(addressof(sb)) is called to install the contained basic_­stringbuf.

29.8.4.2 Assignment and swap [ostringstream.assign]

basic_ostringstream& operator=(basic_ostringstream&& rhs);
Effects: Move assigns the base and members of *this from the base and corresponding members of rhs.
Returns: *this.
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: As if by 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));