std::swap(s...sic_fstream)(3)
π NAME π
okay so bestie, welcome to std::swap(s...sic_fstream)(3) from the C++ Standard Libary manual π
β manual name folded in, spelling kept exactly how it showed up π«Ά.
The name is literally:
std::swap(std::basic_fstream) - std::swap(std::basic_fstream)
β¨ Synopsis β¨
Here's the signature, no cap:
template< class CharT, class Traits >
void swap( basic_fstream<CharT, Traits>& lhs, basic_fstream<CharT,
Traits>& rhs );
So what's the tea? It's the specialization of the std::swap algorithm for std::basic_fstream. It exchanges the state of lhs with that of rhs, and it effectively calls lhs.swap(rhs) β iconic behavior, honestly π.
πΈ Parameters πΈ
lhs,rhsβ streams whose state to swap. That's the whole parameter list, bestie π«Ά.
π«Ά Return value π«Ά
(none) β literally nothing comes back. The return value is (none), and we respect that π.
π Exceptions π
May throw implementation-defined exceptions. That's the caveat, and it's staying right here π.
π¦ Example π¦
Run this code, bestie β here's the whole sample:
// Run this code
#include <fstream>
#include <iostream>
#include <string>
bool create_stream(std::fstream& fs)
{
try
{
std::string some_name = "/tmp/test_file.txt";
std::ios_base::openmode some_flags = fs.trunc; // | other flags
if (std::fstream ts{some_name, some_flags}; ts.is_open())
{
std::swap(ts, fs); // stream objects are not copyable =>
swap
return true;
}
}
catch (...)
{
std::cout << "Exception!\n";
}
return false;
}
int main()
{
if (std::fstream fs; create_stream(fs))
{
// use fs stream
}
}
π See also π
swapβ swaps two file streams (C++11) (public member function) β¨
source/version check: this page came from http://cppreference.com, dated 2024.06.10, for std::swap(s...sic_fstream)(3) β byeee, go swap your streams, mwah π
π¦ 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