std::auto_ptr::operator=(3)
π 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:
- effectively calls
reset(r.release()). that's it. that's the whole thing. - effectively calls
reset(r.release()). but β and this is the important bit βY*must be implicitly convertible toT*. no conversion, no assignment, no cap π§’ - effectively calls
reset(m.release()). andauto_ptr_refis an implementation-defined type that holds a reference toauto_ptr.std::auto_ptris 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β anotherauto_ptrto transfer the ownership of the object from π«Άmβ an object of implementation-defined type that holds a reference toauto_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_ptrwas not assignable fromauto_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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms