function template
<memory>

std::swap (weak_ptr)

template <class T>
  void swap (weak_ptr<T>& x, weak_ptr<T>& y) noexcept;
Exchange content of weak_ptr objects
Exchanges the contents of x with those of y, swapping their owning groups and any stored data.

This non-member function effectively calls x.swap(y).

This is a specialization of the generic algorithm swap.

Parameters

x,y
Two weak_ptr objects of the same type (instantiated with the same template parameter T).

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// weak_ptr swap specialization
#include <iostream>
#include <memory>

int main () {
  std::shared_ptr<int> sp1 (new int(10));
  std::shared_ptr<int> sp2 (new int(20));

  std::weak_ptr<int> wp1(sp1);
  std::weak_ptr<int> wp2(sp2);

  swap(wp1,wp2);

  std::cout << "sp1 -> " << *sp1 << '\n';
  std::cout << "sp2 -> " << *sp2 << '\n';
  std::cout << "wp1 -> " << *wp1.lock() << '\n';
  std::cout << "wp2 -> " << *wp2.lock() << '\n';

  return 0;
}


Output:
foo: 20
bar: 10

Complexity

Constant.

See also