std::auto_ptr::operator=(3)

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

πŸ’… std::auto_ptr::operator=(3) πŸ’…

hey bestie, welcome to section 3 of the C++ Standard Library manual β€” yes, that library, the one with the crustiest assignment operator in the game. sit down, grab a πŸ“, and let's talk about std::auto_ptr::operator=.


✨ NAME ✨

std::auto_ptr::operator= β€” literally just std::auto_ptr::operator=, no notes, iconic behaviour only πŸ’…


πŸ¦‹ Synopsis πŸ¦‹

okay so there are three whole flavors of this thing, and they're all deprecated in C++11 and all removed in C++17. yes, all three. rip bozo 😭

auto_ptr& operator=( auto_ptr& r ) throw();       (1) (deprecated in C++11)
                                                      (removed in C++17)

template< class Y >
auto_ptr& operator=( auto_ptr<Y>& r ) throw();    (2) (deprecated in C++11)
                                                      (removed in C++17)

auto_ptr& operator=( auto_ptr_ref<T> m ) throw(); (3) (deprecated in C++11)
                                                      (removed in C++17)

the whole vibe here is: replaces the managed object with the one managed by r or m. that's the entire assignment. it takes, and it gives you back its old soul. πŸ’€

here's each overload, explained with love:

  1. effectively calls reset(r.release()). that's it. that's the whole thing.
  2. effectively calls reset(r.release()). but β€” and this is the important bit β€” Y* must be implicitly convertible to T*. no conversion, no assignment, no cap 🧒
  3. effectively calls reset(m.release()). and auto_ptr_ref is an implementation-defined type that holds a reference to auto_ptr. std::auto_ptr is implicitly convertible to and from this type. the implementation is allowed to provide the template with a different name or implement equivalent functionality in other ways. iykyk, it's giving "we left this one up to the compilers" energy πŸ™ˆ

πŸŽ€ Parameters πŸŽ€

  • r β€” another auto_ptr to transfer the ownership of the object from 🫢
  • m β€” an object of implementation-defined type that holds a reference to auto_ptr

🌸 Return value 🌸

*this. β€” she gives herself back. main character energy only πŸ‘‘


πŸ”₯ Notes πŸ”₯

okay this is the tea β˜• and honestly it's kind of a slay once you get it.

the constructor and the copy assignment operator from auto_ptr_ref is provided to allow copy-constructing and assigning std::auto_ptr from nameless temporaries. like, temporaries as in no name, as in the little rvalue besties that normally can't be bound. because β€” and here's the plot twist β€” its copy constructor and copy assignment operator take the argument as a non-const reference, so they cannot bind rvalue arguments directly. 😭

but! a user-defined conversion can be executed (which releases the original auto_ptr), followed by a call to the constructor or copy-assignment operator that take auto_ptr_ref by value. so it technically gets there in the end, through the back door.

bestie this is literally an early implementation of move semantics. fr fr. they were doing proto-move-semantics before move semantics was even a thing. respect the elders πŸ’œ

🌈 Defect reports 🌈

the following behavior-changing defect reports were applied retroactively to previously published C++ standards. as in, they went back in time and fixed it. the audacity, i love it ✨

  • LWG 127, applied to C++98 β€” behavior as published: auto_ptr was not assignable from auto_ptr_ref. correct behavior: added overload (3). so overload (3) up there? that one's the patch baby πŸŽ€

and that's the whole page, bestie! no secrets, no hidden flags, no extra options hiding in the walls β€” every last detail is right here. πŸ¦‹

remember: deprecated in C++11, removed in C++17. do not use this in new code, i am begging you. use it only to understand your ancestors. πŸ’€

mwah πŸ’‹ β€” this page came to you from http://cppreference.com, dated 2024.06.10, and it is std::auto_ptr::operator=(3) forever and always. go forth and transfer ownership responsibly 🫢✨

πŸ“¦ where this page comes from