std::unique_ptr::swap
性病::独特[医]PTR::交换
void swap(unique_ptr& other | | (since C++11) |
---|
交换托管对象和相关删除器。*this
另一个unique_ptr
对象other
...
参数
other | - | another unique_ptr object to swap the managed object and the deleter with |
---|
返回值
%280%29
例外
noexcept
规格:
noexcept
例
二次
#include <iostream>
#include <memory>
struct Foo {
Foo(int _val) : val(_val) { std::cout << "Foo...\n"; }
~Foo() { std::cout << "~Foo...\n"; }
int val;
};
int main()
{
std::unique_ptr<Foo> up1(new Foo(1)
std::unique_ptr<Foo> up2(new Foo(2)
up1.swap(up2
std::cout << "up1->val:" << up1->val << std::endl;
std::cout << "up2->val:" << up2->val << std::endl;
}
二次
产出:
二次
Foo...
Foo...
up1->val:2
up2->val:1
~Foo...
~Foo...
二次
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。