|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
unique_ptr::operator= | ||||
| Modifiers | ||||
| Observers | ||||
| Non-member functions | ||||
(C++14)(C++20) | ||||
(until C++20)(C++20) | ||||
(C++20) | ||||
| Helper classes | ||||
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) | |
Deleter is not a reference type, the behavior is undefined ifDeleter is notMoveAssignable, orDeleter would throw an exception.Deleter is a reference type), the behavior is undefined ifstd::remove_reference<Deleter>::type is notCopyAssignable, orDeleter would throw an exception.U is not an array type.unique_ptr<U, E>::pointer is implicitly convertible topointer, and.unique_ptr<T[]>), all following conditions are satisfied:U is an array type.pointer is the same type aselement_type*.unique_ptr<U, E>::pointer is the same type asunique_ptr<U, E>::element_type*.unique_ptr<U, E>::element_type(*)[] is convertible toelement_type(*)[].E is not a reference type, the behavior is undefined if assigningget_deleter() from anrvalue of typeE is ill-formed or would throw an exception.E is a reference type), the behavior is undefined if assigningget_deleter() from anlvalue of typeE is ill-formed or would throw an exception.Contents |
| r | - | smart pointer from which ownership will be transferred |
*this
As a move-only type,unique_ptr's assignment operator only acceptsrvalues arguments (e.g. the result ofstd::make_unique or astd::move'dunique_ptr variable).
#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 1Creating new Foo...Foo 2~Foo 1About to leave inner block...About to leave program...~Foo 2
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 from std::forward<Deleter>(r.get_deleter()) | corrected to std::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 operator was missing the assignability constraint | added the constraint |
| LWG 2246 | C++11 | the assignment target of the converted deleter ofr was not specified | specified asget_deleter() |
| LWG 2899 | C++11 | the move assignment operator was not constrained | constrained |