function template
<memory>
std::swap (shared_ptr)
template <class T>
void swap (shared_ptr<T>& x, shared_ptr<T>& y) noexcept;
Exchange content of shared_ptr objects
Exchanges the contents of x with those of y, transferring ownership of any managed object between them without destroying either.
This non-member function effectively calls x.swap(y).
This is a specialization of the generic algorithm swap.
Parameters
- x,y
- Two shared_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
|
// shared_ptr swap specialization
#include <iostream>
#include <memory>
int main () {
std::shared_ptr<int> foo (new int(10));
std::shared_ptr<int> bar (new int(20));
swap(foo,bar);
std::cout << "foo: " << *foo << '\n';
std::cout << "bar: " << *bar << '\n';
return 0;
}
|
Output:
See also
- shared_ptr::swap
- Swap content (public member function
)
- swap
- Exchange values of two objects (function template
)