最終更新日時(UTC):
が更新

履歴 編集

function
<memory>

std::shared_ptr::swap(C++11)

void swap(shared_ptr& x) noexcept;

概要

他のshared_ptrオブジェクトとデータを入れ替える。

効果

*thisxのデータを入れ替える。

戻り値

なし

例外

投げない

#include <iostream>
#include <memory>

int main()
{
  std::shared_ptr<int> a(new int(3));
  std::shared_ptr<int> b(new int(1));

  std::cout << a << std::endl;
  std::cout << b << std::endl;

  // aとbを入れ替える
  a.swap(b);

  std::cout << a << std::endl;
  std::cout << b << std::endl;
}

出力例

0x14ab010
0x14ab060
0x14ab060
0x14ab010

バージョン

言語

  • C++11

処理系