std::basic_...sic_ostream(3)
π NAME π
std::basic_ostream::~basic_ostream β literally the destructor of std::basic_ostream, and yes bestie the name is the whole name, we're not doing subtitles today π
This page lives in the C++ Standard Library manual, section 3, and it's here to walk you through exactly what this little destructor does (and, iconic behaviour, what it doesn't do) π«Ά
β¨ SYNOPSIS β¨
virtual ~basic_ostream();
Okay so boom β this destroys the basic_ostream object. That's it. That's the whole job π
But here's the part everyone gets wrong, so listen up: this destructor does not perform any operation on the underlying streambuffer (rdbuf()). Like, none. Zero. She's not touching it, no cap π
So who cleans up the buffer? The destructors of the derived output streams, bestie β think std::basic_ofstream and std::basic_ostringstream. Those are the ones responsible for calling the destructors of the stream buffers. They hold it down, periodt π
Fr fr, don't expect basic_ostream's destructor to babysit your streambuf. It simply does not. That is not her main character energy πΈ
π EXAMPLE π
Run this code, it's a whole vibe π₯
// Run this code
#include <iostream>
#include <sstream>
void add_words(std::streambuf* p)
{
std::ostream buf(p); // buf shares the buffer with s
buf << " is the answer";
} // calls the destructor of buf. p remains unaffected
int main()
{
std::ostringstream s;
s << 42;
add_words(s.rdbuf());
s << ".";
std::cout << s.str() << '\n';
}
Notice that little comment, because it's the whole point π β buf shares the buffer with s, and when buf's destructor runs, p remains unaffected. The buffer survives. Slay π¦
π OUTPUT π
42 is the answer.
Ate and left no crumbs, honestly π
And that's the tea, bestie β β this page is straight from http://cppreference.com, dated 2024.06.10, and it answers to the name std::basic_...sic_ostream(3) (the title's a little shy, we love that for her) πβ¨
π¦ 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