std::unique_ptr<T,Deleter>::operator= - cppreference.com (original) (raw)
| unique_ptr& operator=( unique_ptr&& r ) noexcept; | (1) | (constexpr since C++23) |
|---|---|---|
| template< class U, class E >unique_ptr& operator=( unique_ptr<U, E>&& r ) noexcept; | (2) | (constexpr since C++23) |
| unique_ptr& operator=( std::nullptr_t ) noexcept; | (3) | (constexpr since C++23) |
| unique_ptr& operator=( const unique_ptr& ) = delete; | (4) |
- Move assignment operator. Transfers ownership from r to *this as if by calling reset(r.release()) followed by assigning get_deleter() from std::forward<Deleter>(r.get_deleter()).
This overload participates in overload resolution only if std::is_move_assignable<Deleter>::value is true.
If Deleter is not a reference type, the behavior is undefined if
Deleteris not MoveAssignable, or- assigning get_deleter() from an rvalue of type
Deleterwould throw an exception.
Otherwise (Deleter is a reference type), the behavior is undefined if
std::remove_reference<Deleter>::typeis not CopyAssignable, or- assigning get_deleter() from an lvalue of type
Deleterwould throw an exception.
- Converting assignment operator. Transfers ownership from r to *this as if by calling reset(r.release()) followed by assigning get_deleter() from std::forward<E>(r.get_deleter()).
This overload participates in overload resolution only if all following conditions are satisfied:
- std::is_assignable<Deleter&, E&&>::value is true.
- For the primary template, all following conditions are satisfied:
Uis not an array type.unique_ptr<U, E>::pointeris implicitly convertible topointer, and.
- For the array specialization (
unique_ptr<T[]>), all following conditions are satisfied: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(*)[].
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.
Effectively the same as calling reset().
Copy assignment operator is explicitly deleted.
[edit] Parameters
| r | - | smart pointer from which ownership will be transferred |
|---|
[edit] Return value
*this
[edit] 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).
[edit] Example
#include
#include
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 p1(std::make_unique(1));
{
std::cout << "Creating new Foo...\n";
std::unique_ptr p2(std::make_unique(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
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2047 | C++11 | for overload (2), get_deleter() was assigned fromstd::forward<Deleter>(r.get_deleter()) | corrected tostd::forward<E>(r.get_deleter()) |
| LWG 2118 | C++11 | unique_ptr<T[]>::operator=rejected qualification conversions | accepts |
| LWG 2228(N4366) | C++11 | the converting assignment operatorwas missing the assignability constraint | added the constraint |
| LWG 2246 | C++11 | the assignment target of the converteddeleter of r was not specified | specified as get_deleter() |
| LWG 2899 | C++11 | the move assignment operator was not constrained | constrained |