std::unique_ptr::operator=
From cppreference.com
                    
                                        
                    < cpp | memory | unique ptr
                    
                                                            
                    | members of the primary template, unique_ptr<T> | ||
| unique_ptr& operator=( unique_ptr&& r ); | (1) | |
| template< class U, class E > unique_ptr& operator=( unique_ptr<U,E>&& r ); | (1) | |
| unique_ptr& operator=( nullptr_t ); | (2) | |
| members of the specialization for arrays, unique_ptr<T[]> | ||
| unique_ptr& operator=( unique_ptr&& r ); | (1) | |
| template< class U, class E > unique_ptr& operator=( unique_ptr<U,E>&& r ); | (1) | (since C++17) | 
| unique_ptr& operator=( nullptr_t ); | (2) | |
1) Transfers ownership from 
r to *this as if by calling reset(r.release()) followed by an assignment of get_deleter() from std::forward<E>(r.get_deleter()).   If 
Deleter is not a reference type, requires that it is nothrow-MoveAssignable.  If 
Deleter is a reference type, requires that std::remove_reference<Deleter>::type is nothrow-CopyAssignable.  The template version of this assignment operator only participates in overload resolution if 
 U is not an array type and unique_ptr<U,E>::pointer is implicitly convertible to pointer and std::is_assignable<Deleter&, E&&>::value is true (since C++17).|  The template version of this assignment operator in the specialization for arrays,  std::unique_ptr<T[]>behaves the same as in the primary template, except that will only participate in overload resolution if all of the following is true: *  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(*)[] * std::is_assignable<Deleter&, E&&>::value is true
 | (since C++17) | 
2) Effectively the same as calling reset().
Note that unique_ptr's assignment operator only accepts rvalues, which are typically generated by std::move.  (The unique_ptr class explicitly deletes its lvalue copy constructor and lvalue assignment operator.)
Parameters
| r | - | smart pointer from which ownership will be transfered | 
Return value
*this
Exceptions
noexcept specification:  
noexcept
  Example
Run this code
#include <iostream> #include <memory> struct Foo { Foo() { std::cout << "Foo\n"; } ~Foo() { std::cout << "~Foo\n"; } }; int main() { std::unique_ptr<Foo> p1; { std::cout << "Creating new Foo...\n"; std::unique_ptr<Foo> p2(new Foo); // 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:
Creating new Foo... Foo About to leave inner block... About to leave program... ~Foo