std::shared_ptr<T>::owner_equal
来自 cppreference.cn
< cpp | 内存 | shared ptr
template< class Y > bool owner_equal( const std::shared_ptr<Y>& other ) const noexcept; |
(1) | (C++26 起) |
template< class Y > bool owner_equal( const std::weak_ptr<Y>& other ) const noexcept; |
(2) | (C++26 起) |
检查此 shared_ptr
和 other 是否共享所有权,或者是否都为空。比较结果是,两个智能指针仅在它们都为空或它们都拥有同一对象时才比较相等,即使通过 get() 获取的指针值不同(例如,因为它们指向同一对象内不同的子对象)。
成员函数 owner_equal
是一种等价关系,使得 !owner_before(other) && !other.owner_before(*this) 为 true 当且仅当 owner_equal(other) 为 true。
此排序用于使共享指针和弱指针可用作无序关联容器中的键,通常通过 std::owner_equal。
目录 |
[编辑] 参数
其他 | - | 要比较的 std::shared_ptr 或 std::weak_ptr |
[编辑] 返回值
如果 *this 和 other 共享所有权或都为空,则返回 true。否则,返回 false。
[编辑] 注意
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__cpp_lib_smart_ptr_owner_equality |
202306L |
(C++26) | 允许在无序关联容器中使用 std::shared_ptr 作为键 |
[编辑] 示例
本节不完整 原因:示例 |
[编辑] 另请参见
(C++26) |
提供基于所有者的共享指针和弱指针的混合类型相等比较 (类) |