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

履歴 編集

function template
<memory>

std::weak_ptr::owner_equal(C++26)

template <class U>
constexpr bool
  owner_equal(const weak_ptr<U>& b) const noexcept;               // (1) C++26

template <class U>
constexpr bool
  owner_equal(const shared_ptr<U>& b) const noexcept;             // (2) C++26

概要

所有権ベースでの等値比較を行う。

戻り値

*thisbが所有権を共有しているか、両方とも空であればtrueを返し、そうでなければfalseを返す。

以下の式と等価:

!owner_before(b) && !b.owner_before(*this)

備考

詳細は、shared_ptrowner_equal()メンバ関数を参照。

#include <iostream>
#include <memory>

struct X {
  int i;
  int j;
};

int main()
{
  std::shared_ptr<X> org(new X());

  std::shared_ptr<int> sa(org, &(org->i));
  std::shared_ptr<int> sb(org, &(org->j));

  std::weak_ptr<int> wa = sa;
  std::weak_ptr<int> wb = sb;

  std::cout << std::boolalpha;
  std::cout << wa.owner_equal(wb) << std::endl;  // 所有権ベース: true
  std::cout << wa.owner_equal(sa) << std::endl;  // weak_ptrとshared_ptrの比較: true
}

出力

true
true

バージョン

言語

  • C++26

処理系

関連項目

参照