std::weak_ptr::operator=(3)

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

Hey bestie! Welcome to the std::weak_ptr::operator=(3) page from the C++ Standard Libary manual πŸ’–βœ¨ (yes, β€œLibary” is exactly how the manual name is spelled up there β€” we respect the typo, no cap).

πŸ’– NAME πŸ’–

std::weak_ptr::operator= β€” std::weak_ptr::operator=. That’s it, that’s the name. She’s self-titled and honestly? Main character energy πŸ‘‘.

🌸 Synopsis 🌸

Okay so there are five overloads, all chillin’ since C++11 and all noexcept. Here’s the exact lineup, bestie:

weak_ptr& operator=( const weak_ptr& r ) noexcept;      (1) (since C++11)
template< class Y >                                     (2) (since C++11)
weak_ptr& operator=( const weak_ptr<Y>& r ) noexcept;
template< class Y >                                     (3) (since C++11)
weak_ptr& operator=( const shared_ptr<Y>& r ) noexcept;
weak_ptr& operator=( weak_ptr&& r ) noexcept;           (4) (since C++11)
template< class Y >                                     (5) (since C++11)
weak_ptr& operator=( weak_ptr<Y>&& r ) noexcept;

The vibe of the whole thing: replaces the managed object with the one managed by r. The object is shared with r. If r manages no object, *this manages no object too. Periodt. πŸ’…

Specifically:

  • Overloads 1-3) β€” equivalent to std::weak_ptr<T>(r).swap(*this).
  • Overloads 4,5) β€” equivalent to std::weak_ptr<T>(std::move(r)).swap(*this).

πŸŽ€ Parameters πŸŽ€

  • r β€” smart pointer to share an object with πŸ’–

🫢 Return value 🫢

*this. That’s the whole return value. She gives herself back. Iconic.

πŸ“ Notes πŸ“

Implementation tea β˜•: the implementation may meet the requirements without creating a temporary weak_ptr object. No extra temp needed, lowkey efficient.

πŸ’€ Defect reports πŸ’€

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. Yes bestie, retroactive glow-ups are real.

  • DR LWG 2315 β€” applied to C++11. Behavior as published: move semantic was not enabled for weak_ptr. Correct behavior: enabled.

πŸ‘€ See also πŸ‘€

  • constructor β€” creates a new weak_ptr (public member function) ✨
  • swap β€” swaps the managed objects (public member function) πŸ”„

That’s the whole page, bestie 🫢 β€” brought to you by http://cppreference.com, dated 2024.06.10, and filed under std::weak_ptr::operator=(3). Go forth and assign responsibly πŸ’–βœ¨

πŸ“¦ where this page comes from