std::shared_ptr::operator=(3)

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

✨ std::shared_ptr::operator= (3) β€” bestie edition πŸŽ€

okay so this is the glow-up of the std::shared_ptr::operator= page from the C++ Standard Libary manual (yes, β€œLibary” β€” that’s literally how the page spells it πŸ’€). It’s giving overload (3) main character energy, but the page actually covers a whole little family of assignment operators, and we are keeping every single one. no cap.

🌸 NAME

std::shared_ptr::operator= β€” std::shared_ptr::operator=

πŸ“œ Synopsis

shared_ptr& operator=( const shared_ptr& r ) noexcept;    (1)
template< class Y >                                       (2)
shared_ptr& operator=( const shared_ptr<Y>& r ) noexcept;
shared_ptr& operator=( shared_ptr&& r ) noexcept;         (3)
template< class Y >                                       (4)
shared_ptr& operator=( shared_ptr<Y>&& r ) noexcept;
template< class Y >                                       (5)
(deprecated in C++11)
shared_ptr& operator=( std::auto_ptr<Y>&& r );
(removed in C++17)
template< class Y, class Deleter >                        (6)
shared_ptr& operator=( std::unique_ptr<Y, Deleter>&& r );

Replaces the managed object with the one managed by r. 🫢

If *this already owns an object and it is the last shared_ptr owning it, and r is not the same as *this, the object is destroyed through the owned deleter. πŸ’…
  1. (1), (2) β€” Shares ownership of the object managed by r. If r manages no object, *this manages no object too. Equivalent to shared_ptr<T>(r).swap(*this). ✨
  2. (3), (4) β€” Move-assigns a shared_ptr from r. After the assignment, *this contains a copy of the previous state of r, and r is empty. Equivalent to shared_ptr<T>(std::move(r)).swap(*this). πŸ”₯
  3. (5) β€” Transfers the ownership of the object managed by r to *this. If r manages no object, *this manages no object too. After the assignment, *this contains the pointer previously held by r, and use_count() == 1; also r is empty. Equivalent to shared_ptr<T>(r).swap(*this). πŸ¦‹
  4. (6) β€” Transfers the ownership of the object managed by r to *this. The deleter associated to r is stored for future deletion of the managed object. r manages no object after the call. Equivalent to shared_ptr<T>(std::move(r)).swap(*this). πŸ‘‘

πŸ’– Parameters

  • r β€” another smart pointer to share the ownership to or acquire the ownership from.

🫢 Return value

*this β€” that’s the vibe. πŸ’…

🧚 Notes

The implementation may meet the requirements without creating a temporary shared_ptr object. iconic, honestly ✨

😭 Exceptions

  • 5, 6 β€” May throw implementation-defined exceptions.

πŸ™ˆ Example

This section is incomplete Reason: no example

🌈 See also

  • reset β€” replaces the managed object (public member function)

🏷️ Category

  • Todo no example

okay that’s the whole page, bestie. source vibes: http://cppreference.com, dated 2024.06.10, and the footer is literally std::shared_ptr::operator=(3). mwah πŸ’–

πŸ“¦ where this page comes from