std::weak_ptr<T>::owner_equal
来自 cppreference.cn
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_ptr
与 other 是否共享所有权,或是否都为空。该比较使得两个智能指针仅在它们都为空,或它们都拥有同一对象时才比较为等价,即使通过 get() 获得指针值不同(例如因为它们指向同一对象中的不同子对象)。
成员函数 owner_equal
是一种等价关系,使得当且仅当 owner_equal(other) 为 true 时,!owner_before(other) && !other.owner_before(*this) 为 true。
此顺序用于使共享指针和弱指针可用作无序关联容器中的键,通常通过 std::owner_equal 实现。
目录 |
[编辑] 参数
其他 | - | 要比较的 std::shared_ptr 或 std::weak_ptr |
[编辑] 返回值
若 *this 和 other 共享所有权或均为空,则为 true。否则返回 false。
[编辑] 注意
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__cpp_lib_smart_ptr_owner_equality |
202306L |
(C++26) | 允许将 std::weak_ptr 用作无序关联容器中的键 |
[编辑] 示例
本节不完整 原因:示例 |
[编辑] 参阅
(C++26) |
提供基于所有者的共享指针和弱指针的混合类型相等比较 (类) |