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

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

✨ NAME ✨

okay so bestie, this is the page for std::basic_ostream::operator=. It’s from the C++ Standard Libary manual (the header spells it C++ Standard Libary, not Library, and the truncated title is std::basic_...m::operator=(3) β€” iykyk πŸ’€). The NAME is literally:

std::basic_ostream::operator= - std::basic_ostream::operator=

🌈 Synopsis 🌈

Here’s the exact synopsis vibe, bestie:

          protected:                                                     (1)
          basic_ostream& operator=( const basic_ostream& rhs ) = delete;
          protected:                                                     (2)
       (since C++11)
          basic_ostream& operator=( basic_ostream&& rhs );
  1. Overload (1) is the copy assignment operator. It’s protected and it’s deleted. Output streams are not CopyAssignable. πŸ’€ No copy-assigning your streams, bestie.
  2. Overload (2) is the move assignment operator, and it’s since C++11. It exchanges all data members of the base class, except for rdbuf(), with rhs, as if by calling swap(*rhs). This move assignment operator is also protected: it is only called by the move assignment operators of the derived movable output stream classes std::basic_ofstream and std::basic_ostringstream, which know how to correctly move-assign the associated streambuffers. slay.

πŸŽ€ Parameters πŸŽ€

  • rhs β€” the basic_ostream object from which to assign to *this.

πŸ¦‹ Example πŸ¦‹

okay, here’s the code bestie:

// Run this code

#include <iostream>
#include <sstream>
#include <utility>

int main()
{
    std::ostringstream s;
//  std::cout = s;            // ERROR: copy assignment operator is deleted
//  std::cout = std::move(s); // ERROR: move assignment operator is protected
    s = std::move(std::ostringstream() << 42); // OK, moved through derived
    std::cout << s.str() << '\n';
}

πŸ“ Output: πŸ“

42

πŸ’œ Defect reports πŸ’œ

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR: LWG 2067
  • Applied to: C++11
  • Behavior as published:
    1. the parameter type of overload (1) was basic_ostream&
    2. the parameter type of overload (2) was const basic_ostream&&
  • Correct behavior:
    1. added const
    2. removed const

πŸ’– source/version: http://cppreference.com β€’ date: 2024.06.10 β€’ page: std::basic_...m::operator=(3) β€” okay bye bestie, go slay those streams! πŸ’…βœ¨

πŸ“¦ where this page comes from