std::owner_equal

来自cppreference.com
 
 
内存管理库
分配器
内存资源
未初始化存储 (C++20 前*)
垃圾收集器支持 (C++23 前)
 
在标头 <memory> 定义
struct owner_equal;
(C++26 起)

这个函数对象为 std::shared_ptrstd::weak_ptr 提供了基于所有者(而非基于值)的混合类型相等比较。这种比较中,仅当两个智能指针都为空或者共享所有权时比较为等价,即使通过 get() 获取的原生指针值不同(比如由于它们指向同一对象中的不同子对象)也是如此。

这是以 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>

嵌套类型

嵌套类型 定义
is_transparent 未指定

成员函数

使用基于所有者的语义比较各实参
(函数)

std::owner_equal::operator()

template< class T, class U >
constexpr bool operator()( const std::shared_ptr<T>& lhs, 
                           const std::shared_ptr<U>& rhs ) const noexcept;
(1) (C++26 起)
template< class T, class U >
constexpr bool operator()( const std::shared_ptr<T>& lhs, 
                           const std::weak_ptr<U>& rhs ) const noexcept;
(2) (C++26 起)
template< class T, class U >
constexpr bool operator()( const std::weak_ptr<T>& lhs, 
                           const std::shared_ptr<U>& rhs ) const noexcept;
(3) (C++26 起)
template< class T, class U >
constexpr bool operator()( const std::weak_ptr<T>& lhs, 
                           const std::weak_ptr<U>& rhs ) const noexcept;
(4) (C++26 起)

使用基于所有者的语义比较 lhsrhs。相当于调用 lhs.owner_equal(rhs)

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

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

参数

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

返回值

lhsrhs 都为空,或由基于所有者的相等比较确定为共享所有权时返回 true,否则返回 false

注解

功能特性测试 标准 功能特性
__cpp_lib_smart_ptr_owner_equality 202306L (C++26) 使 std::shared_ptrstd::weak_ptr 可用作无序关联容器中的键
__cpp_lib_constexpr_memory 202506L (C++26) constexpr std::owner_equal

参阅

提供基于所有者的共享指针相等比较
(std::shared_ptr<T> 的公开成员函数) [编辑]
提供弱指针的基于所有者的相等比较
(std::weak_ptr<T> 的公开成员函数) [编辑]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.