std::owner_equal
来自 cppreference.cn
定义于头文件 <memory> |
||
struct owner_equal; |
(C++26 起) | |
此函数对象提供基于所有权(而非基于值)的 std::weak_ptr 和 std::shared_ptr 混合类型相等比较。这种比较使得两个智能指针只有在它们都为空,或者它们共享所有权时才被认为是等价的,即使通过 get()
获得的原始指针值不同(例如,因为它们指向同一对象内不同的子对象)。
当使用 std::shared_ptr 和 std::weak_ptr 作为键,并结合 std::owner_hash 构建无序关联容器时,它是首选的比较谓词,例如 std::unordered_map<std::shared_ptr<T>, U, std::owner_hash, std::owner_equal> 或 std::unordered_map<std::weak_ptr<T>, U, std::owner_hash, std::owner_equal>。
3)
std::owner_equal
从参数中推导参数类型。目录 |
[edit] 嵌套类型
嵌套类型 | 定义 |
is_transparent
|
未指定 |
[edit] 成员函数
operator() |
使用基于所有权的语义比较其参数 (函数) |
std::owner_equal::operator()
template< class T, class U > bool operator()( const std::shared_ptr<T>& lhs, |
(C++26 起) | |
template< class T, class U > bool operator()( const std::shared_ptr<T>& lhs, |
(C++26 起) | |
template< class T, class U > bool operator()( const std::weak_ptr<T>& lhs, |
(C++26 起) | |
template< class T, class U > bool operator()( const std::weak_ptr<T>& lhs, |
(C++26 起) | |
使用基于所有权的语义比较 lhs 和 rhs。实际上调用 lhs.owner_equal(rhs)。
相等比较是一种等价关系。
lhs 和 rhs 仅当它们都为空或共享所有权时才等价。
参数
lhs, rhs | - | 要比较的共享所有权指针 |
返回值
true 如果 lhs 和 rhs 都为空或通过基于所有权的相等比较确定共享所有权,否则为 false。
[edit] 注意
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__cpp_lib_smart_ptr_owner_equality |
202306L |
(C++26) | 在无序关联容器中启用将 std::shared_ptr 和 std::weak_ptr 用作键 |
[edit] 参阅
(C++26) |
提供共享指针的基于所有权的相等比较 ( std::shared_ptr<T> 的公共成员函数) |
(C++26) |
提供弱指针的基于所有权的相等比较 ( std::weak_ptr<T> 的公共成员函数) |