命名空间
变体
操作

std::owner_equal

来自 cppreference.cn
< cpp‎ | 内存
 
 
内存管理库
(仅作说明*)
未初始化内存算法
(C++17)
(C++17)
(C++17)
受约束的未初始化
内存算法
C 库

分配器
内存资源
垃圾回收支持
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化存储
(直到 C++20*)
(直到 C++20*)
显式生命周期管理
 
定义于头文件 <memory>
struct owner_equal;
(C++26 起)

此函数对象提供基于所有权(而非基于值)的 std::weak_ptrstd::shared_ptr 混合类型相等比较。这种比较使得两个智能指针只有在它们都为空,或者它们共享所有权时才被认为是等价的,即使通过 get() 获得的原始指针值不同(例如,因为它们指向同一对象内不同的子对象)。

1) 除了 std::shared_ptrstd::weak_ptr 之外的类型不提供基于所有权的混合类型相等比较。
2) std::shared_ptrstd::weak_ptr 的基于所有权的混合类型相等比较。
当使用 std::shared_ptrstd::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,

                 const std::shared_ptr<U>& rhs ) const noexcept;
(C++26 起)
template< class T, class U >

bool operator()( const std::shared_ptr<T>& lhs,

                 const std::weak_ptr<U>& rhs ) const noexcept;
(C++26 起)
template< class T, class U >

bool operator()( const std::weak_ptr<T>& lhs,

                 const std::shared_ptr<U>& rhs ) const noexcept;
(C++26 起)
template< class T, class U >

bool operator()( const std::weak_ptr<T>& lhs,

                 const std::weak_ptr<U>& rhs ) const noexcept;
(C++26 起)

使用基于所有权的语义比较 lhsrhs。实际上调用 lhs.owner_equal(rhs)

相等比较是一种等价关系。

lhsrhs 仅当它们都为空或共享所有权时才等价。

参数

lhs, rhs - 要比较的共享所有权指针

返回值

true 如果 lhsrhs 都为空或通过基于所有权的相等比较确定共享所有权,否则为 false

[edit] 注意

特性测试 标准 特性
__cpp_lib_smart_ptr_owner_equality 202306L (C++26) 无序关联容器中启用将 std::shared_ptrstd::weak_ptr 用作键

[edit] 参阅

提供共享指针的基于所有权的相等比较
(std::shared_ptr<T> 的公共成员函数) [编辑]
提供弱指针的基于所有权的相等比较
(std::weak_ptr<T> 的公共成员函数) [编辑]