public member function
<tuple>

std::unique_ptr::swap

void swap (unique_ptr& x) noexcept;
交換內容
將 unique_ptr 物件的內容與 x 的內容進行交換,在不銷燬任一物件的情況下在它們之間轉移所管理物件的所有權。

該函式透過呼叫它們的 swap 來交換各自的 儲存指標儲存的刪除器

引數

x
同一型別的另一個 unique_ptr 物件(即具有相同的類模板引數)。

返回值



示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// unique_ptr::swap example
#include <iostream>
#include <memory>

int main () {
  std::unique_ptr<int> foo (new int(10));
  std::unique_ptr<int> bar (new int(20));

  foo.swap(bar);

  std::cout << "foo: " << *foo << '\n';
  std::cout << "bar: " << *bar << '\n';

  return 0;
}

輸出
foo: 20
bar: 10


另見