[ostringstream] # 31 Input/output library [[input.output]](./#input.output) ## 31.8 String-based streams [[string.streams]](string.streams#ostringstream) ### 31.8.4 Class template basic_ostringstream [ostringstream] #### [31.8.4.1](#general) General [[ostringstream.general]](ostringstream.general) [🔗](#lib:basic_ostringstream) namespace std {template, class Allocator = allocator>class basic_ostringstream : public basic_ostream {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]](#cons "31.8.4.2 Constructors"), constructors basic_ostringstream() : basic_ostringstream(ios_base::out) {}explicit basic_ostringstream(ios_base::openmode which); explicit basic_ostringstream(const basic_string& s, ios_base::openmode which = ios_base::out); basic_ostringstream(ios_base::openmode which, const Allocator& a); explicit basic_ostringstream( basic_string&& s, ios_base::openmode which = ios_base::out); template basic_ostringstream(const basic_string& s, const Allocator& a): basic_ostringstream(s, ios_base::out, a) {}template basic_ostringstream(const basic_string& s, ios_base::openmode which, const Allocator& a); templateexplicit basic_ostringstream(const basic_string& s, ios_base::openmode which = ios_base::out); templateexplicit basic_ostringstream(const T& t, ios_base::openmode which = ios_base::out); template basic_ostringstream(const T& t, const Allocator& a); template basic_ostringstream(const T& t, ios_base::openmode which, const Allocator& a); basic_ostringstream(const basic_ostringstream&) = delete; basic_ostringstream(basic_ostringstream&& rhs); basic_ostringstream& operator=(const basic_ostringstream&) = delete; basic_ostringstream& operator=(basic_ostringstream&& rhs); // [[ostringstream.swap]](#swap "31.8.4.3 Swap"), swapvoid swap(basic_ostringstream& rhs); // [[ostringstream.members]](#members "31.8.4.4 Member functions"), members basic_stringbuf* rdbuf() const; basic_string str() const &; template basic_string str(const SAlloc& sa) const; basic_string str() &&; basic_string_view view() const noexcept; void str(const basic_string& s); templatevoid str(const basic_string& s); void str(basic_string&& s); templatevoid str(const T& t); private: basic_stringbuf *sb*; // *exposition only*};} [1](#general-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9325) The classbasic_ostringstream supports writing objects of classbasic_string<​charT, traits, Allocator>[.](#general-1.sentence-1) It uses abasic_stringbuf object to control the associated storage[.](#general-1.sentence-2) For the sake of exposition, the maintained data is presented here as: - [(1.1)](#general-1.1) *sb*, the stringbuf object[.](#general-1.sentence-3) #### [31.8.4.2](#cons) Constructors [[ostringstream.cons]](ostringstream.cons) [🔗](#lib:basic_ostringstream,constructor) `explicit basic_ostringstream(ios_base::openmode which); ` [1](#cons-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9347) *Effects*: Initializes the base class withbasic_ostream(addressof(*sb*)) ([[ostream]](ostream "31.7.6.2 Class template basic_­ostream")) and *sb* withbasic_stringbuf(which | ios_base​::​out) ([[stringbuf.cons]](stringbuf.cons "31.8.2.2 Constructors"))[.](#cons-1.sentence-1) [🔗](#lib:basic_ostringstream,constructor_) `explicit basic_ostringstream( const basic_string& s, ios_base::openmode which = ios_base::out); ` [2](#cons-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9363) *Effects*: Initializes the base class withbasic_ostream(addressof(*sb*)) ([[ostream]](ostream "31.7.6.2 Class template basic_­ostream")) and *sb* withbasic_stringbuf(s, which | ios_base​::​out) ([[stringbuf.cons]](stringbuf.cons "31.8.2.2 Constructors"))[.](#cons-2.sentence-1) [🔗](#lib:basic_ostringstream,constructor__) `basic_ostringstream(ios_base::openmode which, const Allocator& a); ` [3](#cons-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9377) *Effects*: Initializes the base class withbasic_ostream(addressof(*sb*)) ([[ostream]](ostream "31.7.6.2 Class template basic_­ostream")) and *sb* withbasic_stringbuf(which | ios_base​::​out, a) ([[stringbuf.cons]](stringbuf.cons "31.8.2.2 Constructors"))[.](#cons-3.sentence-1) [🔗](#lib:basic_ostringstream,constructor___) `explicit basic_ostringstream( basic_string&& s, ios_base::openmode which = ios_base::out); ` [4](#cons-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9393) *Effects*: Initializes the base class withbasic_ostream(addressof(*sb*)) ([[ostream]](ostream "31.7.6.2 Class template basic_­ostream")) and *sb* withbasic_stringbuf(std​::​move(s), which | ios_base​::​​out) ([[stringbuf.cons]](stringbuf.cons "31.8.2.2 Constructors"))[.](#cons-4.sentence-1) [🔗](#lib:basic_ostringstream,constructor____) `template basic_ostringstream( const basic_string& s, ios_base::openmode which, const Allocator& a); ` [5](#cons-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9410) *Effects*: Initializes the base class withbasic_ostream(addressof(*sb*)) ([[ostream]](ostream "31.7.6.2 Class template basic_­ostream")) and *sb* withbasic_stringbuf(s, which | ios_base​::​out, a) ([[stringbuf.cons]](stringbuf.cons "31.8.2.2 Constructors"))[.](#cons-5.sentence-1) [🔗](#lib:basic_ostringstream,constructor_____) `template explicit basic_ostringstream( const basic_string& s, ios_base::openmode which = ios_base::out); ` [6](#cons-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9427) *Constraints*: is_same_v is false[.](#cons-6.sentence-1) [7](#cons-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9431) *Effects*: Initializes the base class withbasic_ostream(addressof(*sb*)) ([[ostream]](ostream "31.7.6.2 Class template basic_­ostream")) and *sb* withbasic_stringbuf(s, which | ios_base​::​out) ([[stringbuf.cons]](stringbuf.cons "31.8.2.2 Constructors"))[.](#cons-7.sentence-1) [🔗](#lib:basic_ostringstream,constructor______) `template explicit basic_ostringstream(const T& t, ios_base::openmode which = ios_base::out); template basic_ostringstream(const T& t, const Allocator& a); template basic_ostringstream(const T& t, ios_base::openmode which, const Allocator& a); ` [8](#cons-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9450) Let which be ios_base​::​out for the overload with no parameter which, anda be Allocator() for the overload with no parameter a[.](#cons-8.sentence-1) [9](#cons-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9455) *Constraints*: is_convertible_v> is true[.](#cons-9.sentence-1) [10](#cons-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9460) *Effects*: Initializes the base class with addressof(*sb*), and direct-non-list-initializes *sb* with t, which | ios_base​::​out, a[.](#cons-10.sentence-1) [🔗](#lib:basic_ostringstream,constructor_______) `basic_ostringstream(basic_ostringstream&& rhs); ` [11](#cons-11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9472) *Effects*: Move constructs from the rvalue rhs[.](#cons-11.sentence-1) This is accomplished by move constructing the base class, and the containedbasic_stringbuf[.](#cons-11.sentence-2) Then calls basic_ostream​::​set_rdbuf(addressof(*sb*)) to install the contained basic_stringbuf[.](#cons-11.sentence-3) #### [31.8.4.3](#swap) Swap [[ostringstream.swap]](ostringstream.swap) [🔗](#lib:swap,basic_ostringstream) `void swap(basic_ostringstream& rhs); ` [1](#swap-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9489) *Effects*: Equivalent to:basic_ostream::swap(rhs);*sb*.swap(rhs.*sb*); [🔗](#lib:swap,basic_ostringstream_) `template void swap(basic_ostringstream& x, basic_ostringstream& y); ` [2](#swap-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9506) *Effects*: Equivalent to x.swap(y)[.](#swap-2.sentence-1) #### [31.8.4.4](#members) Member functions [[ostringstream.members]](ostringstream.members) [🔗](#lib:rdbuf,basic_ostringstream) `basic_stringbuf* rdbuf() const; ` [1](#members-1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9519) *Returns*: const_cast*>(addressof(*sb*))[.](#members-1.sentence-1) [🔗](#lib:str,basic_ostringstream) `basic_string str() const &; ` [2](#members-2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9530) *Effects*: Equivalent to: return rdbuf()->str(); [🔗](#lib:str,basic_ostringstream_) `template basic_string str(const SAlloc& sa) const; ` [3](#members-3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9542) *Effects*: Equivalent to: return rdbuf()->str(sa); [🔗](#lib:str,basic_ostringstream__) `basic_string str() &&; ` [4](#members-4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9553) *Effects*: Equivalent to: return std​::​move(*rdbuf()).str(); [🔗](#lib:view,basic_ostringstream) `basic_string_view view() const noexcept; ` [5](#members-5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9564) *Effects*: Equivalent to: return rdbuf()->view(); [🔗](#lib:str,basic_ostringstream___) `void str(const basic_string& s); ` [6](#members-6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9575) *Effects*: Equivalent to: rdbuf()->str(s); [🔗](#lib:str,basic_ostringstream____) `template void str(const basic_string& s); ` [7](#members-7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9587) *Effects*: Equivalent to: rdbuf()->str(s); [🔗](#lib:str,basic_ostringstream_____) `void str(basic_string&& s); ` [8](#members-8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9598) *Effects*: Equivalent to: rdbuf()->str(std​::​move(s)); [🔗](#lib:str,basic_ostringstream______) `template void str(const T& t); ` [9](#members-9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9610) *Constraints*: is_convertible_v> is true[.](#members-9.sentence-1) [10](#members-10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9615) *Effects*: Equivalent to: rdbuf()->str(t);