Namespaces
Variants

std:: 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)
定义于头文件 <memory>
struct owner_equal ;
(C++26 起)

此函数对象提供基于所有者的(而非基于值的)混合类型相等比较,适用于 std::weak_ptr std::shared_ptr 。其比较逻辑为:两个智能指针仅当同时为空或共享所有权时才被视为等价,即使通过 get() 获取的原始指针值不同(例如因为它们指向同一对象中的不同子对象)。

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

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 起)

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

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

仅当 lhs rhs 均为空或共享所有权时,它们才是等价的。

参数

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

返回值

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

注释

功能测试 标准 功能
__cpp_lib_smart_ptr_owner_equality 202306L (C++26) 允许将 std::shared_ptr std::weak_ptr 用作 无序关联容器 的键值

参阅

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