std::owner_less - cppreference.com
来自cppreference.com
| 在标头 |
||
template< class T > struct owner_less; /* 未定义 */ |
(1) | (C++11 起) (C++17 前) |
template< class T = void > struct owner_less; /* 未定义 */ |
(C++17 起) | |
template< class T > struct owner_less<std::shared_ptr<T>>; |
(2) | (C++11 起) |
template< class T > struct owner_less<std::weak_ptr<T>>; |
(3) | (C++11 起) |
template<> struct owner_less<void>; |
(4) | (C++17 起) |
此函数对象提供基于拥有者(而不是基于值)的,std::weak_ptr 和 std::shared_ptr 两者的混合类型定序。这种排序中满足仅当两个智能指针均为空或共享所有权时才会比较等价,即使由 get() 获得的裸指针值不同(例如因为它们指向同一对象中的不同子对象)也是如此。
1) 不会为 std::shared_ptr 和 std::weak_ptr 以外的类型提供基于拥有者的混合类型定序。
2) std::shared_ptr 的基于拥有者的混合类型定序。
在以 std::shared_ptr 为键建立关联容器时,最好使用此比较断言,即 std::map<std::shared_ptr<T>, U, std::owner_less<std::shared_ptr<T>>>。
3) std::weak_ptr 的基于拥有者的混合类型定序。
在以 std::weak_ptr 为键建立关联容器时,最好使用此比较断言,即 std::map<std::weak_ptr<T>, U, std::owner_less<std::weak_ptr<T>>>。
4) void 特化会从各实参推导出形参类型。
默认的 operator< 并没有为弱指针提供定义,并且它可能错误地认为指向同一对象的两个共享指针不等价(见 std::shared_ptr::owner_before)。
特化标准库提供 |
(C++17 起) |
嵌套类型
|
(C++20 前) |
成员函数
std::owner_less::operator()
| std::shared_ptr 特化才有的成员 |
||
bool operator()( const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs ) const noexcept; |
(1) | (C++11 起) (C++26 起为 constexpr) |
| std::weak_ptr 特化才有的成员 |
||
bool operator()( const std::weak_ptr<T>& lhs, const std::weak_ptr<T>& rhs ) const noexcept; |
(2) | (C++11 起) (C++26 起为 constexpr) |
| std::shared_ptr 和 std::weak_ptr 特化都有的成员 |
||
bool operator()( const std::shared_ptr<T>& lhs, const std::weak_ptr<T>& rhs ) const noexcept; |
(3) | (C++11 起) (C++26 起为 constexpr) |
bool operator()( const std::weak_ptr<T>& lhs, const std::shared_ptr<T>& rhs ) const noexcept; |
(4) | (C++11 起) (C++26 起为 constexpr) |
用基于拥有者的语义比较 lhs 和 rhs。等价于调用 lhs.owner_before(rhs)。
此顺序是严格弱序关系。
仅当 lhs 和 rhs 均为空或共享所有权时它们才会相等。
参数
返回值
在按基于拥有者的顺序确定 lhs 小于 rhs 时返回 true,否则返回 false。
注解
| 功能特性测试宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_constexpr_memory |
202506L |
(C++26) | constexpr std::owner_less
|
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2873 | C++11 | operator() 不需要是 noexcept 的
|
需要是 noexcept 的 |