std::weak_ptr<T>::owner_equal

< cpp‎ | memory‎ | weak ptr

template< class Y >
bool owner_equal( const std::weak_ptr<Y>& other ) const noexcept;
(1) (C++26 起)
template< class Y >
bool owner_equal( const std::shared_ptr<Y>& other ) const noexcept;
(2) (C++26 起)

检查此 weak_ptrother 是否共享所有权或均为空。比较方式使得两个智能指针当且仅当它们均为空或拥有同一对象时比较相等,即使从 get() 获得的指针值不同(例如因为它们指向同一对象的不同子对象)也是如此。

成员函数 owner_equal 是一种等价关系,当且仅当 owner_equal(other)true!owner_before(other) && !other.owner_before(*this)true

此顺序常通过 std::owner_equal 使用,使得共享指针与弱指针能用作无序关联容器的键。

参数

other - 要比较的 std::shared_ptrstd::weak_ptr

返回值

*thisother 共享所有权或均为空则为 true。否则返回 false

注解

功能特性测试 标准 功能特性
__cpp_lib_smart_ptr_owner_equality 202306L (C++26) 启用以 std::weak_ptr无序关联容器中的键

示例

参阅

(C++26)
提供共享指针与弱指针的基于拥有者的混合类型相等比较
(类)