public member function
<tuple>

std::unique_ptr::swap

void swap (unique_ptr& x) noexcept;
Swap content
Exchanges the contents of the unique_ptr object with those of x, transferring ownership of any managed object between them without destroying either.

The function swaps the respective stored pointers and stored deleters by invoking swap on them.

Parameters

x
Another unique_ptr object of the same type (i.e., with the same class template parameters).

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// unique_ptr::swap example
#include <iostream>
#include <memory>

int main () {
  std::unique_ptr<int> foo (new int(10));
  std::unique_ptr<int> bar (new int(20));

  foo.swap(bar);

  std::cout << "foo: " << *foo << '\n';
  std::cout << "bar: " << *bar << '\n';

  return 0;
}


Output:
foo: 20
bar: 10

See also