std::basic_ios::rdbuf(3)

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

πŸ’– NAME πŸ’–

okay bestie, welcome to std::basic_ios::rdbuf from the C++ Standard Libary manual, section 3 🫢. The name is literally std::basic_ios::rdbuf - std::basic_ios::rdbuf β€” yes, it said it twice, and honestly? iconic. This page is all about managing the associated stream buffer πŸ’….

🌈 Synopsis 🌈

std::basic_streambuf<CharT, Traits>* rdbuf() const;

(1)

std::basic_streambuf<CharT, Traits>* rdbuf(
std::basic_streambuf<CharT, Traits>* sb );

(2)

Manages the associated stream buffer. Here's the breakdown, bestie:

  • 1) Returns the associated stream buffer. If there is no associated stream buffer, returns a null pointer.
  • 2) Sets the associated stream buffer to sb. The error state is cleared by calling clear(). Returns the associated stream buffer before the operation. If there is no associated stream buffer, returns a null pointer.

πŸŽ€ Parameters πŸŽ€

  • sb β€” stream buffer to associate to.

πŸ’… Return value πŸ’…

The associated stream buffer, or a null pointer if there was no associated stream buffer. periodt ✨.

πŸ”₯ Exceptions πŸ”₯

May throw implementation-defined exceptions. so yeah, implementation-defined is the whole mood there πŸ™ˆ.

πŸ¦‹ Example πŸ¦‹

The example starts with // Run this code β€” and honestly it's a little main-character moment. It saves std::cout's buffer, swaps in a local ostringstream buffer, prints into it so you don't see it, restores the old buffer, then prints again and dumps the local content. Copy it exactly, bestie:

#include <iostream>
#include <sstream>

int main()
{
    std::ostringstream local;
    auto cout_buff = std::cout.rdbuf(); // save pointer to std::cout buffer

    std::cout.rdbuf(local.rdbuf()); // substitute internal std::cout buffer with
        // buffer of 'local' object

    // now std::cout work with 'local' buffer
    // you don't see this message
    std::cout << "some message";

    // go back to old buffer
    std::cout.rdbuf(cout_buff);

    // you will see this message
    std::cout << "back to default buffer\n";

    // print 'local' content
    std::cout << "local content: " << local.str() << "\n";
}

🌸 Output 🌸

back to default buffer
local content: some message

πŸ‘‘ See also πŸ‘‘

  • set_rdbuf β€” replaces the rdbuf without clearing its error state (protected member function).

mwah πŸ’‹ β€” source/version/date vibes: http://cppreference.com, 2024.06.10, page std::basic_ios::rdbuf(3). periodt 🫢✨

πŸ“¦ where this page comes from