std::swap(s...c_ofstream)(3)

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

✨ NAME ✨

okay bestie, this is the page for std::swap(std::basic_ofstream) from the C++ Standard Libary manual β€” yes, that’s the manual name, spelling and all, and I’m not touching it πŸ’…. The page title is literally std::swap(std::basic_ofstream), and the NAME line is:

std::swap(std::basic_ofstream) - std::swap(std::basic_ofstream)


🌸 Synopsis 🌸

template< class CharT, class Traits >
void swap( basic_ofstream<CharT, Traits>& lhs, basic_ofstream<CharT,
Traits>& rhs
);

This specializes the std::swap algorithm for std::basic_ofstream. It exchanges the state of lhs with that of rhs. Effectively, it calls lhs.swap(rhs). slay.


πŸŽ€ Parameters πŸŽ€

  • lhs, rhs β€” streams whose state to swap 🫢

πŸ¦‹ Return value πŸ¦‹

(none) β€” that’s it, bestie, nothing comes back πŸ’–


⭐ Exceptions ⭐

May throw implementation-defined exceptions.


πŸ”₯ Example πŸ”₯

the example literally starts with // Run this code, so here’s the whole thing exactly as it appears:

// Run this code

#include <fstream>
#include <iostream>
#include <string>

bool create_stream(std::ofstream& fs)
{
    try
    {
        std::string some_name = "/tmp/test_file.txt";
        std::ios_base::openmode some_flags = fs.trunc; // | other flags

        if (std::ofstream 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::ofstream fs; create_stream(fs))
    {
        // use fs stream
    }
}

πŸ’œ See also πŸ’œ

  • swap β€” swaps two file streams (C++11) (public member function) ✨

πŸ’– that’s the whole page, bestie β€” brought to you by http://cppreference.com, dated 2024.06.10, with the footer title std::swap(s...c_ofstream)(3). periodt πŸ’–

πŸ“¦ where this page comes from