std::basic_...asic_ostream(3)
π NAME π
std::basic_ostream::basic_ostream β okay so it's literally the constructor for std::basic_ostream, like, the whole vibe of this page is how do you even make one of these. It's the std::basic_ostream::basic_ostream constructor, bestie. That's the name. That's the whole thing. β¨
(this is living in the C++ Standard Libary manual β and yes that's exactly how it's spelled up top, we are not judging, we are simply observing π )
π Synopsis π
explicit basic_ostream( std::basic_streambuf<CharT, Traits>* sb );
(1)
protected:
(2) (since C++11)
basic_ostream( const basic_ostream& rhs ) = delete;
protected:
(3) (since C++11)
basic_ostream( basic_ostream&& rhs );
So there's three flavors, and here's the tea on each one π₯
- Constructs the
basic_ostreamobject, assigning initial values to the base class by callingbasic_ios::init(sb). - The copy constructor is
protected, and is deleted. Output streams are not copyable. Not even a little bit. Not even if you beg. π - The move constructor uses
basic_ios<CharT, Traits>::move(rhs)to move allbasic_iosmembers, except for therdbuf(), fromrhsinto*this. This move constructor isprotected: it is called by the move constructors of movable output stream classesstd::basic_ofstreamandstd::basic_ostringstream, which know how to correctly move the associated streambuffer. Like, they're the ones with the actual qualifications for this job, iykyk. π
π Parameters π
sbβ streambuffer to use as output sequencerhsβbasic_ostreamto initialize from
π¦ Notes π¦
Because basic_ios::init(sb) sets badbit when sb is a null pointer, and because basic_ostream::sentry does nothing if the stream is already in a failed state, writing to a stream constructed from a null pointer sb is a no-op. Literally nothing happens. You write, the stream looks at you, the stream does not care. The stream is unbothered. π
πΈ Example πΈ
// Run this code
#include <iostream>
#include <sstream>
#include <utility>
int main()
{
// ERROR: copy ctor is deleted
// std::ostream myout(std::cout);
// OK: shares buffer with cout
std::ostream myout(std::cout.rdbuf());
// ERROR: move constructor is protected
// std::ostream s2(std::move(std::ostringstream() << 7.1));
// OK: move ctor called through the derived class
std::ostringstream s2(std::ostringstream() << 7.1);
myout << s2.str() << '\n';
std::ostream dev_null{nullptr}; // see Notes above
dev_null << "no-op";
}
Output:
7.1
Notice how the commented-out lines are the ones throwing hands π₯ β copy ctor deleted, move ctor protected, neither one is gonna work for you directly. But std::ostringstream? That's the main character, that one's got you covered. π«Ά And dev_null justβ¦ eats the string and says nothing. Iconic behaviour honestly. β
that's the whole page, bestie β go forth and construct responsibly π
Source: http://cppreference.com, dated 2024.06.10, from the std::basic_...asic_ostream(3) page. 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