std::basic_filebuf::swap(3)
β¨ std::basic_filebuf::swap(3) β¨
Okay so we're in the C++ Standard Libary β that's the manual name, bestie β and today's main character is std::basic_filebuf::swap. π This page is all about swapping the state and contents of two file buffers, and it's giving very specific C++ energy.
πΈ NAME πΈ
std::basic_filebuf::swap - std::basic_filebuf::swap. Yes, the NAME section is just the name twice; iconic. β¨
π Synopsis π
void swap( std::basic_filebuf& rhs ); (since C++11)
Swaps the state and the contents of *this and rhs. No cap, that's the whole move. π₯
π Parameters π
rhsβ anotherbasic_filebuf. That's the one parameter, bestie. π«Ά
π¦ Return value π¦
(none) β no return value here. It's void, so don't go looking for a return gift. π
π Notes π
This function is called automatically when swapping std::fstream objects, so the stream machinery handles it for you. Because of that, the page says it is rarely necessary to call it directly. Lowkey a background character, highkey still important. π
π Example π
The original says // Run this code β here's the whole thing, exactly as the page gives it:
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::ifstream fin("test.in"); // read-only
std::ofstream fout("test.out"); // write-only
std::string s;
getline(fin, s);
std::cout << s << '\n'; // outputs the first line of test.in
fin.rdbuf()->swap(*fout.rdbuf()); //swap the underlying buffers
getline(fin, s); // fails: cannot read from a write-only filebuf
std::cout << s << '\n'; // prints empty line
}
Vibe check on the example: fin is read-only, fout is write-only, and after the underlying buffers get swapped, trying to getline from fin fails because now it's basically trying to read from a write-only filebuf. The output is an empty line. π
π See also π
operator=β assigns abasic_filebufobject.(C++11)(public member function)std::swap(std::basic_filebuf)β specializes thestd::swapalgorithm.(C++11)(function template)swapβ swaps two file streams.(C++11)(public member function ofstd::basic_fstream<CharT,Traits>)
Source/version/date sign-off, bestie: this page came from http://cppreference.com, dated 2024.06.10, and it's the std::basic_filebuf::swap(3) manual page. Bye! π
π¦ 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