std::strstream::strstream(3)
π std::strstream::strstream(3) β C++ Standard Libary bestie briefing π
okay so this is the std::strstream::strstream(3) page from the C++ Standard Libary, and we are getting into constructor lore. Itβs giving βold-school C++ stream energy,β and yes, bestie, the deprecation/removal notes are absolutely part of the story. π
πΈ NAME πΈ
std::strstream::strstream β std::strstream::strstream
β¨ Synopsis β¨
Two constructor overloads, both with big deprecated in C++98 / removed in C++26 energy.
strstream();
Overload (1): deprecated in C++98, removed in C++26.
strstream( char* s, int n, std::ios_base::openmode mode =
std::ios_base::in |
std::ios_base::out );
Overload (2): also deprecated in C++98 and removed in C++26.
This constructs a new input/output strstream and its underlying std::strstreambuf. π«Ά
- Default-constructs the underlying
std::strstreambuf, which creates a dynamically growing buffer, and initializes the base class with the address of thestrstreambufmember. - Initializes the base class with the address of the underlying
std::strstreambufmember, which is initialized in one of the two possible ways below, and both of them use a user-provided fixed-size array:- a) if
(mode & app) == 0(theappbit is not set inmode), constructs the buffer by callingstrstreambuf(s, n, s). The behavior is undefined if there are less thannelements in the array whose first element is pointed to bys. - b) if
(mode & app) != 0(theappbit is set inmode), constructs the buffer by callingstrstreambuf(s, n, s + std::strlen(s)). The behavior is undefined if there are less thannelements in the array whose first element is pointed to bysor if the array does not contain a valid null-terminated character sequence.
- a) if
π Parameters π
sβchararray to use as the output buffer.nβ size of the array to be used for output.modeβ specifies stream open mode. It is a bitmask type, and the following constants are defined (although onlyappis used):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.
π Example π
The page says // Run this code, so here is the whole example, bestie:
#include <iostream>
#include <string>
#include <strstream>
int main()
{
// dynamic buffer
std::strstream s1;
s1 << 1 << ' ' << 3.14 << " example" << std::ends;
std::cout << "Buffer holds: '" << s1.str() << "'\n";
s1.freeze(false);
int n;
double d;
std::string w;
s1 >> n >> d >> w;
std::cout << "Read back: n = " << n
<< ", d = " << d
<< ", w = '" << w << "'\n";
// static buffer
char arr[20] = "-1 -3.14 ";
std::strstream s2(arr, sizeof arr, std::ios_base::app);
s2 << "another" << std::ends;
std::cout << "Buffer holds: '" << s2.str() << "'\n";
s2 >> n >> d >> w;
std::cout << "Read back: n = " << n
<< ", d = " << d
<< ", w = '" << w << "'\n";
}
π«Ά Output π«Ά
Buffer holds: '1 3.14 example'
Read back: n = 1, d = 3.14, w = 'example'
Buffer holds: '-1 -3.14 another'
Read back: n = -1, d = -3.14, w = 'another'
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR
LWG 115β applied toC++98. Behavior as published: overload (2) only considered the casemode & app == 0(==has higher precedence than&). Correct behavior: considers cases(mode & app) == 0and(mode & app) != 0.
π₯ See also π₯
- constructor β constructs a
strstreambufobject (public member function ofstd::strstreambuf). - constructor β constructs an
istrstreamobject, optionally allocating the buffer (public member function ofstd::istrstream). - constructor β constructs an
ostrstreamobject, optionally allocating the buffer (public member function ofstd::ostrstream).
okay bestie, thatβs the whole page! Source/version energy: http://cppreference.com, dated 2024.06.10, and the page is std::strstream::strstream(3). Periodt. πβ¨
π¦ 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