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
从实参推导参数类型。目录 |
[编辑] 嵌套类型
嵌套类型 | 定义 |
is_transparent
|
未指定 |
[编辑] 成员函数
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 | - | 要比较的共享所有权指针 |
返回值
如果 lhs 和 rhs 都为空或共享所有权(由基于所有权的相等比较确定),则为 true,否则为 false。
[编辑] 注解
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_smart_ptr_owner_equality |
202306L |
(C++26) | 允许在无序关联容器中使用 std::shared_ptr 和 std::weak_ptr 作为键 |
[编辑] 参见
(C++26) |
提供共享指针的基于所有权的相等比较 ( std::shared_ptr<T> 的公共成员函数) |
(C++26) |
提供弱指针的基于所有权的相等比较 ( std::weak_ptr<T> 的公共成员函数) |