Skip to content

Latest commit

 

History

History
70 lines (52 loc) · 1.09 KB

File metadata and controls

70 lines (52 loc) · 1.09 KB

swap

  • memory[meta header]
  • std[meta namespace]
  • weak_ptr[meta class]
  • function[meta id-type]
  • cpp11[meta cpp]
void swap(weak_ptr& r) noexcept;

概要

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

効果

*thisrの監視対象を入れ替える。

戻り値

なし

例外

投げない

#include <iostream>
#include <memory>

int main()
{
  std::shared_ptr<int> sp1(new int(1));
  std::weak_ptr<int> wp1 = sp1;

  std::shared_ptr<int> sp2(new int(2));
  std::weak_ptr<int> wp2 = sp2;

  // wp1とwp2を入れ替える
  wp1.swap(wp2);

  if (std::shared_ptr<int> r = wp1.lock()) {
    std::cout << *r << std::endl;
  }

  if (std::shared_ptr<int> r = wp2.lock()) {
    std::cout << *r << std::endl;
  }
}
  • swap[color ff0000]
  • lock()[link lock.md]

出力

2
1

バージョン

言語

  • C++11

処理系