std::basic_stringbuf<CharT,Traits,Allocator>::basic_stringbuf - cppreference.com (original) (raw)

(1)
explicit basic_stringbuf( std::ios_base::openmode which = std::ios_base::in | std::ios_base::out ); (until C++11)
explicit basic_stringbuf( std::ios_base::openmode which ); (since C++11)
basic_stringbuf() : basic_stringbuf( std::ios_base::in | std::ios_base::out ) {} (2) (since C++11)
explicit basic_stringbuf( const std::basic_string<CharT, Traits, Allocator>& s, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out ); (3)
explicit basic_stringbuf( std::basic_string<CharT, Traits, Allocator>&& s, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out ); (4) (since C++20)
basic_stringbuf( std::ios_base::openmode which, const Allocator& a ); (5) (since C++20)
explicit basic_stringbuf( const Allocator& a ) : basic_stringbuf( std::ios_base::in | std::ios_base::out, a ) {} (6) (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 ); (7) (since C++20)
template< class SAlloc > basic_stringbuf( const std::basic_string<CharT, Traits, SAlloc>& s, std::ios_base::openmode which, const Allocator& a ); (8) (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 ) {} (9) (since C++20)
template< class StringViewLike > explicit basic_stringbuf( const StringViewLike& t, std::ios_base::openmode which = std::ios_base::in | std::ios_base::out ); (10) (since C++26)
template< class StringViewLike > basic_stringbuf( const StringViewLike& t, std::ios_base::openmode which, const Allocator& a ); (11) (since C++26)
template< class StringViewLike >basic_stringbuf( const StringViewLike& t, const Allocator& a ); (12) (since C++26)
basic_stringbuf( basic_stringbuf&& rhs ); (13) (since C++11)
basic_stringbuf( basic_stringbuf&& rhs, const Allocator& a ); (14) (since C++20)
basic_stringbuf( const basic_stringbuf& rhs ) = delete; (15) (since C++11)

The std::basic_streambuf base and the exposition-only data members _buf_ and _mode_ are initialized as follows.

After initializing these subobjects, overloads (3-12) initialize the input and output sequences as if by calling init_buf_ptrs().

Overload std::basic_streambuf base buf mode
(1) default-initialized implementation-defined(see below) which
(2) std::ios_base::in | std::ios_base::out
(3) s which
(4) std::move(s)
(5) a
(6) std::ios_base::in | std::ios_base::out
(7) s which
(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 (eback(), gptr(), egptr(), pbase(), pptr(), epptr()) are initialized to null pointers.

5,6) When the construction is complete, str.empty() is true.

  1. 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.

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

  1. The copy constructor is deleted; std::basic_stringbuf is not CopyConstructible.

[edit] Parameters

s - a std::basic_string used to initialize the buffer
t - an object (convertible to std::basic_string_view) used to initialize the buffer
a - another allocator used to construct the internal std::basic_string
rhs - another basic_stringbuf
which - specifies stream open mode. It is bitmask type, the following constants are defined: Constant Explanation app seek to the end of stream before each write binary open in binary mode in open for reading out open for writing trunc discard the contents of the stream when opening ate seek to the end of stream immediately after open noreplace (C++23) open in exclusive mode

[edit] 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 Value Std Feature
__cpp_lib_sstream_from_string_view 202306L (C++26) Interfacing string streams with std::string_view

[edit] Example

Demonstrates calling the constructor of std::basic_stringbuf directly:

Output:

1 test1 est12 (Sun Studio) 2st1 (GCC)

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 432 C++98 1. overload (1) allocated no array object2. overload (3) did not specify how the input and output sequences are initialized 1. removed the limitation2. specified
LWG 562 C++98 overload (3) set epptr() to point one past the last underlyingcharacter if bool(which & std::ios_base::out) == true epptr() can be setbeyond that position
P0935R0 C++11 the default constructor was explicit made implicit

[edit] See also

| | constructs the string stream (public member function of std::basic_stringstream<CharT,Traits,Allocator>) [edit] | | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |