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

履歴 編集

function
<memory>

std::weak_ptr::コンストラクタ(C++11)

constexpr weak_ptr() noexcept;             // (1)
weak_ptr(const weak_ptr& r) noexcept;      // (2)

template <class Y>
weak_ptr(const weak_ptr<Y>& r) noexcept;   // (3)

template <class Y>
weak_ptr(const shared_ptr<Y>& r) noexcept; // (4)

weak_ptr(weak_ptr&& r) noexcept;           // (5) C++14

template <class Y>
weak_ptr(weak_ptr<Y>&& r) noexcept;        // (6) C++14

weak_ptrオブジェクトの構築

  • (1) : デフォルトコンストラクタ。監視対象を持たない、空のweak_ptrオブジェクトを構築する。
  • (2) : コピーコンストラクタ。監視対象をコピーする。
  • (3) : 変換可能なweak_ptrの監視対象をコピーする。
  • (4) : shared_ptrオブジェクトrを監視する。
  • (5) : ムーブコンストラクタ。監視対象をrから*thisに移動する。
  • (6) : 変換可能なweak_ptrからのムーブコンストラクタ。監視対象をrから*thisに移動する。

要件

  • (3), (4), (6) C++11 : Y*T*に暗黙変換可能であること。そうでない場合、これらはオーバーロード解決の候補から外れる。
  • (3), (4), (6) C++17 : Y*T*と互換であること。そうでない場合、これらはオーバーロード解決の候補から外れる。

効果

  • (1) : 監視対象を持たない、空のweak_ptrオブジェクトを構築する。
  • (2), (3) : rが空である場合、空のweak_ptrオブジェクトを構築する。そうでなければ、rの監視対象を*thisにコピーする。
  • (4) : r*thisの監視対象とする。
  • (5), (6) : rの監視対象を*thisに移動する。

事後条件

#include <cassert>
#include <iostream>
#include <memory>

int main()
{
  // (1)
  // 監視対象を持たない、空のweak_ptrオブジェクトを構築する。
  std::weak_ptr<int> wp1;
  assert(wp1.use_count() == 0);

  // (2)
  // 他のweak_ptrオブジェクトの監視対象をコピーする
  std::shared_ptr<int> sp2(new int(3));
  std::weak_ptr<int> wp2_org = sp2; // wp2_orgはsp2を監視する
  std::weak_ptr<int> wp2 = wp2_org; // wp2_orgの監視対象をコピーする

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

  // (3)
  // 変換可能なweak_ptrオブジェクトの監視対象をコピーする
  std::shared_ptr<int> sp3(new int(3));
  std::weak_ptr<int> wp3_org = sp3;
  std::weak_ptr<void> wp3 = wp3_org;

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

  // (4)
  // shared_ptrオブジェクトを監視する
  std::shared_ptr<int> sp4(new int(3));
  std::weak_ptr<int> wp4 = sp4;

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

  // (5)
  // weak_ptrオブジェクトの監視対象を移動する
  std::shared_ptr<int> sp5(new int(3));
  std::weak_ptr<int> wp5_org = sp5;
  std::weak_ptr<int> wp5 = std::move(wp5_org);

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

  // (6)
  // 変換可能なweak_ptrオブジェクトの監視対象を移動する
  std::shared_ptr<int> sp6(new int(3));
  std::weak_ptr<int> wp6_org = sp6;
  std::weak_ptr<void> wp6 = std::move(wp6_org);

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

出力

3
3
3
3
3

バージョン

言語

  • C++11

処理系

参照