std::basic_...asic_ostream(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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 πŸ”₯

  1. Constructs the basic_ostream object, assigning initial values to the base class by calling basic_ios::init(sb).
  2. The copy constructor is protected, and is deleted. Output streams are not copyable. Not even a little bit. Not even if you beg. πŸ™ˆ
  3. The move constructor uses basic_ios<CharT, Traits>::move(rhs) to move all basic_ios members, except for the rdbuf(), from rhs into *this. This move constructor is protected: it is called by the move constructors of movable output stream classes std::basic_ofstream and std::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 sequence
  • rhs β€” basic_ostream to 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