std::basic_ios::rdbuf(3)
π 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 tosb. The error state is cleared by callingclear(). 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 therdbufwithout 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
- 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