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).
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:
See also
- shared_ptr::swap
- Swap content (public member function
)
- swap
- Exchange values of two objects (function template
)