Namespaces
Variants

std::weak_ptr<T>:: owner_equal

From cppreference.net

Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
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_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::weak_ptr 作为键值

示例

参见

提供共享指针和弱指针的混合类型基于所有者的相等比较
(类)