public member function
<sstream>

std::basic_ostringstream::basic_ostringstream

default (1)
explicit basic_ostringstream (ios_base::openmode which = ios_base::out);
initialization (2)
explicit basic_ostringstream (    const basic_string<char_type,traits_type,allocator_type>& str,    ios_base::openmode which = ios_base::out);
default (1)
explicit basic_ostringstream (ios_base::openmode which = ios_base::out);
initialization (2)
explicit basic_ostringstream (    const basic_string<char_type,traits_type,allocator_type>& str,    ios_base::openmode which = ios_base::out);
copy (3)
basic_ostringstream (const basic_ostringstream&) = delete;
move (4)
basic_ostringstream (basic_ostringstream&& x);
Construct object
Constructs a basic_ostringstream object:

(1) empty constructor (default constructor)
Constructs a basic_ostringstream object with an empty sequence as content.
Internally, its basic_ostream base constructor is passed a pointer to a basic_stringbuf object constructed with an argument based on which.
(2) initialization constructor
Constructs a basic_ostringstream object with a copy of str as content.
Internally, its basic_ostream base constructor is passed a pointer to a basic_stringbuf object constructed with values based on str and which as arguments.
(3) copy constructor (deleted)
Deleted (no copy constructor).
(4) move constructor
Acquires the contents of x.
First, the function move-constructs both its base basic_ostream class from x and a basic_stringbuf object from x's internal basic_streambuf object, and then associates them by calling member set_rdbuf.
x is left in an unspecified but valid state.
It is unspecified whether the sequence controlled by the internal basic_stringbuf object is the one in x before the call, or a copy of it. In any case, both objects have internal string buffers that use independent sequences after the call.
The internal basic_stringbuf object has at least the same duration as the basic_ostringstream object.

Parameters

str
A basic_string object with the same template parameters (charT, traits and Alloc), whose content is copied.
x
A basic_ostringstream object of the same type (with the same class template parameters charT, traits and Alloc), whose value is moved.
which
Open mode: Access given by the internal basic_stringbuf object to its internal sequence of characters. It is an object of member type openmode for which any combination of the following member values is significant:
member constantstands foraccess
ios_base::in*inputThe sequence supports input operations.
ios_base::outoutputThe sequence supports output operations.
Other values of type ios_base::openmode may also be specified, although whether they have an effect on basic_ostringstream objects depends on the library implementation.
member constantstands foraccess
ios_base::in*inputThe sequence supports input operations.
ios_base::outoutputThe sequence supports output operations.
ios_base::ateat endThe writing position is at the end after construction, and after every time the content is reset with member str.
Other values of type ios_base::openmode (such as ios_base::app) may also be specified, although whether they have an effect on basic_ostringstream objects depends on the library implementation.
* ios_base::out is always set for basic_ostringstream objects (even if explicitly not set in argument which).
Note that even though basic_ostringstream is an output stream, its internal basic_stringbuf object may be set to also support input operations.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ostringstream constructor
#include <iostream>     // std::cout, std::ios
#include <sstream>      // std::ostringstream

int main () {
  std::ostringstream foo;                            // out
  std::ostringstream bar (std::ostringstream::ate);  // out|ate

  foo.str("Test string");
  bar.str("Test string");

  foo << 101;
  bar << 101;

  std::cout << foo.str() << '\n';
  std::cout << bar.str() << '\n';
  return 0;
}

Output:
101t string
Test string101


Data races

The move constructor (4) modifies x.

Exception safety

Strong guarantee: if an exception is thrown, there are no side effects.

See also