Scroll to navigation

std::basic_stringbuf::basic_stringbuf(3) C++ Standard Libary std::basic_stringbuf::basic_stringbuf(3)

NAME

std::basic_stringbuf::basic_stringbuf - std::basic_stringbuf::basic_stringbuf

Synopsis


basic_stringbuf()
: basic_stringbuf(std::ios_base::in | (1) (since C++11)
std::ios_base::out) { }
explicit basic_stringbuf( std::ios_base::openmode
which = (until C++11)
std::ios_base::in | std::ios_base::out );
explicit basic_stringbuf( std::ios_base::openmode (since C++11)
which );
explicit basic_stringbuf( const
std::basic_string<CharT, Traits, Allocator>& s,
(3)
std::ios_base::openmode which =


std::ios_base::in | std::ios_base::out );
basic_stringbuf( const basic_stringbuf& rhs ) = (4) (since C++11)
delete;
basic_stringbuf( basic_stringbuf&& rhs ); (5) (since C++11)
explicit basic_stringbuf( const Allocator& a )
: basic_stringbuf(std::ios_base::in | (6) (since C++20)
std::ios_base::out, a) { }
basic_stringbuf( std::ios_base::openmode which, (7) (since C++20)
const Allocator& a );
explicit basic_stringbuf( std::basic_string<CharT,
Traits, Allocator>&& s,
(8) (since C++20)
std::ios_base::openmode which =
(2)
std::ios_base::in | std::ios_base::out );
template<class SAlloc>


basic_stringbuf( const std::basic_string<CharT,
Traits, SAlloc>& s, (9) (since C++20)
const Allocator& a )


: basic_stringbuf(s, std::ios_base::in |
std::ios_base::out, a ) { }
template<class SAlloc>


basic_stringbuf( const std::basic_string<CharT, (10) (since C++20)
Traits, SAlloc>& s,


std::ios_base::openmode, const Allocator& a );
template<class SAlloc>


explicit basic_stringbuf( const
std::basic_string<CharT, Traits, SAlloc>& s, (11) (since C++20)
std::ios_base::openmode which =


std::ios_base::in | std::ios_base::out );
basic_stringbuf( basic_stringbuf&& rhs, const (12) (since C++20)
Allocator& a );


1) Default constructor. It is implementation-defined whether the sequence pointers
(eback(), gptr(), egptr(), pbase(), pptr(), epptr()) are initialized to null
pointers.
2) Constructs a std::basic_stringbuf object: initializes the base class by calling
the default constructor of std::basic_streambuf, initializes the character sequence
with an empty string, and sets the mode to which.
3) Constructs a std::basic_stringbuf object by performing the same initialization as
(1), followed by initializing the associated character sequence as if by calling
str(s).
4) The copy constructor is deleted; std::basic_stringbuf is not CopyConstructible
5) Move-constructs a std::basic_stringbuf object by moving all state from another
std::basic_stringbuf object rhs, including the associated string, the open mode, the
locale, and all other state. After the move, the six pointers of
std::basic_streambuf in *this are guaranteed to be different from the corresponding
pointers in the moved-from rhs unless null.
6-7) Same as (1-2), except that a is used to construct the associated string.
8) Same as (3), except that std::move(s) is used to construct the associated string.
9-10) Constructs a std::basic_stringbuf object by performing the same initialization
as (1), followed by initializing the associated string with content of s, using a as
its allocator.
11) Same as (3), except the type of s's allocator is not Allocator. This overload
participates in overload resolution only if SAlloc and Allocator are not the same
type.
12) Same as (5), except that a is used to construct the associated string.

Parameters


s - a basic_string used to initialize the buffer
a - another allocator used to construct the internal basic_string
rhs - another basic_stringbuf
specifies stream open mode. It is bitmask type, the following constants are
defined:


Constant Explanation
which - 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

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.

Example


Demonstrates calling the constructor of 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.


DR Applied to Behavior as published Correct behavior
P0935R0 C++11 default constructor was explicit made implicit

See also


constructs the string stream
constructor (public member function of
std::basic_stringstream<CharT,Traits,Allocator>)

2022.07.31 http://cppreference.com