std::basic_...f::operator=(3)

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

πŸ’… std::basic_...f::operator=(3) πŸ’…

okay bestie, welcome to the C++ Standard Libary manual page for std::basic_filebuf::operator= β€” yes, that's the assignment operator for basic_filebuf, and it's kind of a two-overload situation. One is a move-assignment slay, one is deleted. Let's get into it.


✨ NAME ✨

std::basic_filebuf::operator= - std::basic_filebuf::operator=


🌸 Synopsis 🌸

std::basic_filebuf& operator=( std::basic_filebuf&& rhs );
(1) (since C++11)
std::basic_filebuf& operator=( const std::basic_filebuf& rhs ) =
(2)
delete;

okay so this operator assigns another basic_filebuf object. The two overloads are doing very different things:

  1. This is the move-assignment one, (1) (since C++11). First it calls close() to close the associated file, then it moves the contents of rhs into *this: the put and get buffers, the associated file, the locale, the openmode, the is_open flag, and any other state. After the move, rhs is not associated with a file and rhs.is_open() == false.
  2. The copy assignment operator is deleted β€” literally delete; in the synopsis β€” and basic_filebuf is not CopyAssignable.
no cap, the deleted copy one is a whole vibe: if you try to copy-assign a basic_filebuf, the compiler is gonna shut that down.

πŸ’– Parameters πŸ’–

  • rhs β€” another basic_filebuf that will be moved from.

πŸŽ€ Return value πŸŽ€

*this β€” that's the return, bestie.


πŸ¦‹ Example πŸ¦‹

cppreference has the Run this code link, and the manpage literally shows // Run this code. Here's the example exactly as it appears:

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

        int main()
        {
            std::ofstream{"test.in"} << "test\n"; // writes via a temporary
       object
            std::ifstream fin("test.in"); // read-only stream
            std::ofstream fout("test.out"); // write-only stream

            std::string s;
            std::getline(fin, s);
            std::cout << "s = [" << s << "]\n"; // s contains "test"

            assert(fout.is_open());
            *fin.rdbuf() = std::move(*fout.rdbuf());
            assert(!fout.is_open());

            std::getline(fin, s);
            std::cout << "s = [" << s << "]\n"; // s is empty input
        }
yes, the object line is the original wrap β€” I left it exactly alone because details matter, bestie.

πŸ“ Output πŸ“

s = [test]
s = []

πŸ‘‘ See also πŸ‘‘

  • constructor β€” constructs a basic_filebuf object (public member function).
  • swap β€” swaps two basic_filebuf objects (C++11) (public member function).

okay that's the whole page, bestie 🫢 β€” source: http://cppreference.com, version/date: 2024.06.10. mwah πŸ’‹

πŸ“¦ where this page comes from