std::unique_ptr::operator=(3)
π std::unique_ptr::operator=(3) π
okay so this is from the C++ Standard Libary manual, bestie, and we're talking about the assignment operator for std::unique_ptr. it's giving ownership-transfer energy, and honestly? iconic. π
πΈ NAME πΈ
std::unique_ptr::operator= β std::unique_ptr::operator=. yes, that's the name section. it's the assignment operator. periodt.
π Synopsis π
okay so the overload lineup is literally this:
unique_ptr& operator=( unique_ptr&& r ) noexcept; (1)
(constexpr since C++23)
template< class U, class E > (2)
(constexpr since C++23)
unique_ptr& operator=( unique_ptr<U, E>&& r ) noexcept;
unique_ptr& operator=( std::nullptr_t ) noexcept; (3)
(constexpr since C++23)
unique_ptr& operator=( const unique_ptr& ) = delete; (4)
- Move assignment operator. It transfers ownership from
rto*thisas if by callingreset(r.release())followed by assigningget_deleter()fromstd::forward<Deleter>(r.get_deleter()). This overload participates in overload resolution only ifstd::is_move_assignable<Deleter>::valueistrue.
If Deleter is not a reference type, the behavior is undefined if
Deleteris notMoveAssignable, or- assigning
get_deleter()from an rvalue of typeDeleterwould throw an exception.
Otherwise (Deleter is a reference type), the behavior is undefined if
std::remove_reference<Deleter>::typeis notCopyAssignable, or- assigning
get_deleter()from an lvalue of typeDeleterwould throw an exception.
- Converting assignment operator. It transfers ownership from
rto*thisas if by callingreset(r.release())followed by assigningget_deleter()fromstd::forward<E>(r.get_deleter()).
For the primary template, this overload participates in overload resolution only if
Uis not an array type,unique_ptr<U, E>::pointeris implicitly convertible topointer, andstd::is_assignable<Deleter&, E&&>::valueistrue.
For the array specialization (unique_ptr<T[]>), this overload participates in overload resolution only if
Uis an array type,pointeris the same type aselement_type*,unique_ptr<U, E>::pointeris the same type asunique_ptr<U, E>::element_type*,unique_ptr<U, E>::element_type(*)[]is convertible toelement_type(*)[], andstd::is_assignable<Deleter&, E&&>::valueistrue.
If E is not a reference type, the behavior is undefined if assigning get_deleter() from an rvalue of type E is ill-formed or would throw an exception. Otherwise (E is a reference type), the behavior is undefined if assigning get_deleter() from an lvalue of type E is ill-formed or would throw an exception.
std::nullptr_toverload. Effectively the same as callingreset(). that's the whole vibe. β¨- Copy assignment operator. It is explicitly deleted. not me trying to copy a
unique_ptrβ bestie, it's deleted, okay? π
π§Έ Parameters π§Έ
rβ smart pointer from which ownership will be transferred. that's the one doing the handing-over.
π«Ά Return value π«Ά
*this. obviously. π
π¦ Notes π¦
As a move-only type, unique_ptr's assignment operator only accepts rvalues arguments (e.g. the result of std::make_unique or a std::move'd unique_ptr variable). no lvalue shenanigans, no cap.
π§ͺ Example π§ͺ
// Run this code
#include <iostream>
#include <memory>
struct Foo
{
int id;
Foo(int id) : id(id) { std::cout << "Foo " << id << '\n'; }
~Foo() { std::cout << "~Foo " << id << '\n'; }
};
int main()
{
std::unique_ptr<Foo> p1(std::make_unique<Foo>(1));
{
std::cout << "Creating new Foo...\n";
std::unique_ptr<Foo> p2(std::make_unique<Foo>(2));
// p1 = p2; // Error ! can't copy unique_ptr
p1 = std::move(p2);
std::cout << "About to leave inner block...\n";
// Foo instance will continue to live,
// despite p2 going out of scope
}
std::cout << "About to leave program...\n";
}
π» Output π»
Foo 1
Creating new Foo...
Foo 2
~Foo 1
About to leave inner block...
About to leave program...
~Foo 2
π΅οΈ Defect reports π΅οΈ
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- LWG 2047, applied to C++11: behavior as published: for overload (2),
get_deleter()was assigned fromstd::forward<E>(r.get_deleter()); correct behavior: corrected tostd::forward<Deleter>(r.get_deleter()). - LWG 2118, applied to C++11: behavior as published:
unique_ptr<T[]>::operator=rejected qualification conversions; correct behavior: accepts. - LWG 2228, applied to C++11: behavior as published: the converting assignment operator was not constrained; correct behavior: constrained.
- LWG 2899, applied to C++11: behavior as published: the move assignment operator was not constrained; correct behavior: constrained.
kisses and sparkles π«Ά β this page came from http://cppreference.com, stamped 2024.06.10, and it's std::unique_ptr::operator=(3). bye bestie! π
π
π¦ 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