std::basic_...ic_stringbuf(3)
πΈ NAME
okay so bestie, this is the page for std::basic_stringbuf::basic_stringbuf β literally the constructor of std::basic_stringbuf. The header calls it std::basic_...ic_stringbuf(3), and it comes from the C++ Standard Libary manual (yes, βLibaryβ with one r, we are not fixing it π
). The NAME line is also just std::basic_stringbuf::basic_stringbuf twice with a dash, because apparently they wanted to make absolutely sure you got it.
β¨ Synopsis β¨
Hereβs the overload squad, exactly as the page lays it out, version tags included. No cap, all of these are real:
// (1) (until C++11)
explicit basic_stringbuf( std::ios_base::openmode which =
std::ios_base::in |
std::ios_base::out );
// (2) (since C++11)
explicit basic_stringbuf( std::ios_base::openmode which );
basic_stringbuf()
: basic_stringbuf( std::ios_base::in | std::ios_base::out ) {}
// (3)
explicit basic_stringbuf( const std::basic_string<CharT, Traits, Allocator>& s,
std::ios_base::openmode which =
std::ios_base::in |
std::ios_base::out );
// (4) (since C++20)
explicit basic_stringbuf( std::basic_string<CharT, Traits, Allocator>&& s,
std::ios_base::openmode which =
std::ios_base::in |
std::ios_base::out );
// (5) (since C++20)
basic_stringbuf( std::ios_base::openmode which, const Allocator& a );
// (6) (since C++20)
explicit basic_stringbuf( const Allocator& a )
: basic_stringbuf( std::ios_base::in | std::ios_base::out, a ) {}
// (7) (since C++20)
template< class SAlloc >
explicit basic_stringbuf( const std::basic_string<CharT, Traits, SAlloc>& s,
std::ios_base::openmode which =
std::ios_base::in |
std::ios_base::out );
// (8) (since C++20)
template< class SAlloc >
basic_stringbuf( const std::basic_string<CharT, Traits, SAlloc>& s,
std::ios_base::openmode which, const Allocator& a );
// (9) (since C++20)
template< class SAlloc >
basic_stringbuf( const std::basic_string<CharT, Traits, SAlloc>& s,
const Allocator& a )
: basic_stringbuf( s, std::ios_base::in | std::ios_base::out, a ) {}
// (10) (since C++26)
template< class StringViewLike >
explicit basic_stringbuf( const StringViewLike& t,
std::ios_base::openmode which =
std::ios_base::in |
std::ios_base::out );
// (11) (since C++26)
template< class StringViewLike >
basic_stringbuf( const StringViewLike& t,
std::ios_base::openmode which, const Allocator& a );
// (12) (since C++26)
template< class StringViewLike >
basic_stringbuf( const StringViewLike& t, const Allocator& a );
// (13) (since C++11)
basic_stringbuf( basic_stringbuf&& rhs );
// (14) (since C++20)
basic_stringbuf( basic_stringbuf&& rhs, const Allocator& a );
// (15) (since C++11)
basic_stringbuf( const basic_stringbuf& rhs ) = delete;
π§ the init deets
The std::basic_streambuf base and the exposition-only data members buf and mode are initialized like this:
- (1) β
std::basic_streambufbase:implementation-defined (see below);buf:default-initialized;mode:which. - (2) β
std::basic_streambufbase:implementation-defined (see below);buf:default-initialized;mode:std::ios_base::in | std::ios_base::out. - (3) β
std::basic_streambufbase:default-initialized;buf:s;mode:which. - (4) β
std::basic_streambufbase:default-initialized;buf:std::move(s);mode:which. - (5) β
std::basic_streambufbase:default-initialized;buf:a;mode:which. - (6) β
std::basic_streambufbase:default-initialized;buf:a;mode:std::ios_base::in | std::ios_base::out. - (7) β
std::basic_streambufbase:default-initialized;buf:s;mode:which. - (8) β
std::basic_streambufbase:default-initialized;buf:{s, a};mode:which. - (9) β
std::basic_streambufbase:default-initialized;buf:{s, a};mode:std::ios_base::in | std::ios_base::out. - (10) β
std::basic_streambufbase:default-initialized;buf:{sv, Allocator()};mode:which. - (11) β
std::basic_streambufbase:default-initialized;buf:{sv, a};mode:which. - (12) β
std::basic_streambufbase:default-initialized;buf:{sv, a};mode:std::ios_base::in | std::ios_base::out. - (13) β
std::basic_streambufbase:rhs;buf:std::move(rhs).str();mode:rhs.mode. - (14) β
std::basic_streambufbase:(copy constructed);buf:{std::move(rhs).str(), a};mode:rhs.mode.
After initializing these subobjects, overloads (3-12) initialize the input and output sequences as if by calling init_buf_ptrs().
1,2)β Overload(1)(until C++11) and(2)(since C++11) is the default constructor. It is implementation-defined whether the sequence pointers (eback(),gptr(),egptr(),pbase(),pptr(),epptr()) are initialized to null pointers.5,6)β When the construction is complete,str.empty()is true.7)β This overload participates in overload resolution only ifstd::is_same_v<SAlloc, Allocator>is false.10-12)β Implicitly convertstto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then it is used as above in the table. These overloads participate in overload resolution only ifstd::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>is true.13,14)β Overload(13)is the move constructor. It is implementation-defined whether the six sequence pointers in*thisobtain the values whichrhshad. When the construction is complete,rhsis empty but usable, and:- Let
rhs_prefer to the state ofrhsjust prior to this construction, the following expressions will evaluate to true:str() == rhs_p.str()getloc() == rhs_p.getloc()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()
- Let
rhs_arefer to the state ofrhsjust after this construction, the following expressions will evaluate to true:!eback() || eback() != rhs_a.eback()!gptr() || gptr() != rhs_a.gptr()!egptr() || egptr() != rhs_a.egptr()!pbase() || pbase() != rhs_a.pbase()!pptr() || pptr() != rhs_a.pptr()!epptr() || epptr() != rhs_a.epptr()
- Let
15)β The copy constructor is deleted;std::basic_stringbufis not CopyConstructible.
π Parameters π
sβ astd::basic_stringused to initialize the buffer.tβ an object (convertible tostd::basic_string_view) used to initialize the buffer.aβ another allocator used to construct the internalstd::basic_string.rhsβ anotherbasic_stringbuf.whichβ specifies stream open mode. It is bitmask type, the following constants are defined:appβ seek to the end of stream before each writebinaryβ open in binary modeinβ open for readingoutβ open for writingtruncβ discard the contents of the stream when openingateβ seek to the end of stream immediately after opennoreplace(C++23) β open in exclusive mode
π«Ά Notes π«Ά
Typically called by the constructor of std::basic_stringstream.
The level of support for the open modes other than std::ios_base::in and std::ios_base::out varies among implementations. C++11 explicitly specifies the support for std::ios_base::ate in str() and in this constructor, but std::ios_base::app, std::ios_base::trunc, and std::ios_base::binary have different effects on different implementations.
- Feature-test macro:
__cpp_lib_sstream_from_string_viewβ value202306Lβ standard(C++26)β feature: Interfacing string streams withstd::string_view.
π Example π
Demonstrates calling the constructor of std::basic_stringbuf directly:
Run this code:
#include <iostream>
#include <sstream>
int main()
{
// default constructor (mode = in | out)
std::stringbuf buf1;
buf1.sputc('1');
std::cout << &buf1 << '\n';
// string constructor in at-end mode (C++11)
std::stringbuf buf2("test", std::ios_base::in
| std::ios_base::out
| std::ios_base::ate);
buf2.sputc('1');
std::cout << &buf2 << '\n';
// append mode test (results differ among compilers)
std::stringbuf buf3("test", std::ios_base::in
| std::ios_base::out
| std::ios_base::app);
buf3.sputc('1');
buf3.pubseekpos(1);
buf3.sputc('2');
std::cout << &buf3 << '\n';
}
Output:
1
test1
est12 (Sun Studio) 2st1 (GCC)
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 432β applied toC++98β behavior as published: 1. overload(1)allocated no array object; 2. overload(3)did not specify how the input and output sequences are initialized. Correct behavior: 1. removed the limitation; 2. specified.LWG 562β applied toC++98β behavior as published: overload(3)setepptr()to point one past the last underlying character ifbool(which & std::ios_base::out) == true. Correct behavior:epptr()can be set beyond that position.P0935R0β applied toC++11β behavior as published: the default constructor wasexplicit. Correct behavior: made implicit.
π See also π
- constructor β constructs the string stream (public member function of
std::basic_stringstream<CharT,Traits,Allocator>).
okay bestie thatβs the whole page π source/version/date tea: http://cppreference.com, 2024.06.10, and the page is std::basic_...ic_stringbuf(3). byeee π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms