std::unique_ptr<T,Deleter>::swap
From cppreference.com
 
 
 < cpp | memory | unique ptr 
 
 
 C++ 
 Feature test macros (C++20)
 Concepts library (C++20)
 Metaprogramming library (C++11)
 Ranges library (C++20)
 Filesystem library (C++17)
 Concurrency support library (C++11)
 Execution control library (C++26)
Memory management library 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
(exposition only*)
(C++11)
(C++23)
(C++11)
(C++17)
(C++11)
(C++11)
(C++20)
(C++20)
(C++17)
(C++11)
(C++17)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
(C++20)
(C++20)
(C++20)
 (C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
 Uninitialized storage (until C++20)
(until C++20*)
(until C++20*)
(until C++20*)
 Garbage collector support (until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)
(C++11)
(C++17)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
(until C++17*)
(C++11)
(C++17)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
std::unique_ptr 
 
 
 
 
unique_ptr::swap
(C++14)(C++20)
(until C++20)(C++20)
(C++20)
void swap( unique_ptr& other ) noexcept;
 
 (since C++11) 
Swaps the managed objects and associated deleters of *this and another unique_ptr object other.
[edit] Parameters
 other
 -
 another unique_ptr object to swap the managed object and the deleter with
[edit] Return value
(none)
[edit] Example
Run this code
#include <iostream> #include <memory> struct Foo { Foo(int _val) : val(_val) { std::cout << "Foo...\n"; } ~Foo() { std::cout << "~Foo...\n"; } int val; }; int main() { std::unique_ptr <Foo> up1(new Foo(1)); std::unique_ptr <Foo> up2(new Foo(2)); up1.swap(up2); std::cout << "up1->val:" << up1->val << '\n'; std::cout << "up2->val:" << up2->val << '\n'; }
Output:
Foo... Foo... up1->val:2 up2->val:1 ~Foo... ~Foo...