function template
<memory>
std::swap (unique_ptr)
template <class T, class D>void swap (unique_ptr<T,D>& x, unique_ptr<T,D>& y) noexcept;
Exchange content of unique_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 unique_ptr objects of the same type (instantiated with the same template parameters).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// unique_ptr swap specialization
#include <iostream>
#include <memory>
int main () {
std::unique_ptr<int> foo (new int(10));
std::unique_ptr<int> bar (new int(20));
swap(foo,bar);
std::cout << "foo: " << *foo << '\n';
std::cout << "bar: " << *bar << '\n';
return 0;
}
|
Output:
See also
- unique_ptr::swap
- Swap content (public member function)
- swap
- Exchange values of two objects (function template)