template <class U>
constexpr bool
owner_equal(const shared_ptr<U>& b) const noexcept; // (1) C++26
template <class U>
constexpr bool
owner_equal(const weak_ptr<U>& b) const noexcept; // (2) C++26
概要
所有権ベースでの等値比較を行う。
戻り値
*thisとbが所有権を共有しているか、両方とも空であればtrueを返し、そうでなければfalseを返す。
以下の式と等価:
!owner_before(b) && !b.owner_before(*this)
備考
owner_before()が所有権ベースでの順序を提供するのに対し、owner_equal()は所有権ベースでの等値判定を提供する。
owner_hash()と組み合わせて、shared_ptrやweak_ptrをunordered_mapやunordered_setのキーとして使用できる。
例
#include <iostream>
#include <memory>
struct X {
int i;
int j;
};
int main()
{
std::shared_ptr<X> org(new X());
// 別名コンストラクタで異なるポインタを指すが、所有権は同じ
std::shared_ptr<int> a(org, &(org->i));
std::shared_ptr<int> b(org, &(org->j));
std::cout << std::boolalpha;
std::cout << (a == b) << std::endl; // 値ベース: false
std::cout << a.owner_equal(b) << std::endl; // 所有権ベース: true
}
出力
false
true
バージョン
言語
- C++26
処理系
- Clang: 22 ✅
- GCC: 16 ✅
- Visual C++: 2026 Update 2 ❌