explicitbasic_stringbuf(std::ios_base::openmodewhich=std::ios_base::in|std::ios_base::out); (1)(until C++11)explicitbasic_stringbuf(std::ios_base::openmodewhich);(since C++11)basic_stringbuf():basic_stringbuf(std::ios_base::in|std::ios_base::out){} (2) (since C++11)explicitbasic_stringbuf(conststd::basic_string<CharT,Traits,Allocator>&s,std::ios_base::openmodewhich=std::ios_base::in|std::ios_base::out); (3) explicitbasic_stringbuf(std::basic_string<CharT,Traits,Allocator>&&s,std::ios_base::openmodewhich=std::ios_base::in|std::ios_base::out); (4) (since C++20)basic_stringbuf(std::ios_base::openmodewhich,constAllocator&a); (5) (since C++20)explicitbasic_stringbuf(constAllocator&a):basic_stringbuf(std::ios_base::in|std::ios_base::out,a){} (6) (since C++20)template<classSAlloc>explicitbasic_stringbuf(conststd::basic_string<CharT,Traits,SAlloc>&s,std::ios_base::openmodewhich=std::ios_base::in|std::ios_base::out); (7) (since C++20)template<classSAlloc>basic_stringbuf(conststd::basic_string<CharT,Traits,SAlloc>&s,std::ios_base::openmodewhich,constAllocator&a); (8) (since C++20)template<classSAlloc>basic_stringbuf(conststd::basic_string<CharT,Traits,SAlloc>&s,constAllocator&a):basic_stringbuf(s,std::ios_base::in|std::ios_base::out,a){} (9) (since C++20)template<classStringViewLike>explicitbasic_stringbuf(constStringViewLike&t,std::ios_base::openmodewhich=std::ios_base::in|std::ios_base::out); (10) (since C++26)template<classStringViewLike>basic_stringbuf(constStringViewLike&t,std::ios_base::openmodewhich,constAllocator&a); (11) (since C++26)template<classStringViewLike>basic_stringbuf(constStringViewLike&t,constAllocator&a); (12) (since C++26)basic_stringbuf(basic_stringbuf&&rhs); (13) (since C++11)basic_stringbuf(basic_stringbuf&&rhs,constAllocator&a); (14) (since C++20)basic_stringbuf(constbasic_stringbuf&rhs)=delete; (15) (since C++11)The
base and the
buf and mode are initialized as follows.
After initializing these subobjects, overloads (3-12) initialize the input and output sequences as if by calling
.
Overload
base bufmode(1)default-initialized implementation-defined
(see below) which(2)std::ios_base::in|
std::ios_base::out(3)swhich(4)std::move(s)(5)a(6)std::ios_base::in|
std::ios_base::out(7)swhich(8){s,a}(9)std::ios_base::in|
std::ios_base::out(10){sv,Allocator()}which(11){sv,a}(12)std::ios_base::in|
std::ios_base::out(13)rhs
(copy constructed) std::move(rhs).str()rhs.mode(14){std::move(rhs).str(),a}1,2) Overload (1)(until C++11)(2)(since C++11) is the default constructor. It is implementation-defined whether the sequence pointers (
,
,
,
,
,
) are initialized to null pointers.
5,6) When the construction is complete, str.empty() is true.
7) This overload participates in overload resolution only if std::is_same_v<SAlloc,Allocator> is false.
10-12) Implicitly converts t to a string view sv as if by std::basic_string_view<CharT,Traits>sv=t;, then it is used as above in the table.
These overloads participate in overload resolution only if std::is_convertible_v<constStringViewLike&,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 *this obtain the values which rhs had.
When the construction is complete, rhs is empty but usable, and
Let rhs_p refer to the state of rhs just 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_a refer to the state of rhs just 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()
15) The copy constructor is deleted; std::basic_stringbuf is not
.
Parameters
s - a
used to initialize the buffer t - an object (convertible to
) used to initialize the buffer a - another allocator used to construct the internal
rhs - another basic_stringbufwhich - specifies stream open mode. It is bitmask type, the following constants are defined: Constant Explanation
seek to the end of stream before each write
open in
open for reading
open for writing
discard the contents of the stream when opening
seek to the end of stream immediately after open
(C++23)open in exclusive mode Notes
Typically called by the constructor of
.
The level of support for the open modes other than
and
varies among implementations. C++11 explicitly specifies the support for
in
and in this constructor, but
,
, and
have different effects on different implementations.
macro ValueStdFeature
__cpp_lib_sstream_from_string_view
(C++26)Interfacing string streams with
Example
Demonstrates calling the constructor of std::basic_stringbuf directly:
Run this code
#include<iostream>#include<sstream>intmain(){// default constructor (mode = in | out)std::stringbufbuf1;buf1.sputc('1');std::cout<<&buf1<<'\n';// string constructor in at-end mode (C++11)std::stringbufbuf2("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::stringbufbuf3("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.
DR Applied to Behavior as published Correct behavior
C++98 1. overload (1) allocated no array object
2. overload (3) did not specify how the input
and output sequences are initialized 1. removed the limitation
2. specified
C++98 overload (3) set
to point one past the last underlying
character if bool(which&std::ios_base::out)==true
can be set
beyond that position
C++11 the default constructor was explicit made implicit See also
constructs the string stream
(public member function of std::basic_stringstream<CharT,Traits,Allocator>)