public member function
<memory>

std::shared_ptr::swap

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

Parameters

x
Another shared_ptr object of the same type (i.e., with the same class template parameter T).

Return value

none

Example

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

int main () {
  std::shared_ptr<int> foo (new int(10));
  std::shared_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