std::owner_less
来自 cppreference.cn
定义于头文件 <memory> |
||
(1) | ||
template< class T > struct owner_less; /* 未定义 */ |
(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()
获取的原始指针值不同(例如,因为它们指向同一对象中的不同子对象)。
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 前) |
[编辑] 成员函数
operator() |
使用基于所有权的语义比较其参数 (函数) |
std::owner_less::operator()
仅为特化 (2) 的成员 |
||
bool operator()( const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs ) const noexcept; |
(C++11 起) | |
仅为特化 (3) 的成员 |
||
bool operator()( const std::weak_ptr<T>& lhs, const std::weak_ptr<T>& rhs ) const noexcept; |
(C++11 起) | |
两个模板特化共享的成员 |
||
bool operator()( const std::shared_ptr<T>& lhs, const std::weak_ptr<T>& rhs ) const noexcept; |
(C++11 起) | |
bool operator()( const std::weak_ptr<T>& lhs, const std::shared_ptr<T>& rhs ) const noexcept; |
(C++11 起) | |
使用基于所有权的语义比较 lhs 和 rhs。等效于调用 lhs.owner_before(rhs)。
此排序是严格弱排序关系。
lhs 和 rhs 仅当它们都为空或共享所有权时才等价。
参数
lhs, rhs | - | 要比较的共享所有权指针 |
返回值
若 lhs 根据基于所有权的排序“小于”rhs 则为 true,否则为 false。
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 2873 | C++11 | operator() 未要求为 noexcept | 要求为 noexcept |
[编辑] 另请参阅
提供共享指针的基于所有权的排序 ( std::shared_ptr<T> 的 public 成员函数) | |
提供弱指针的基于所有权的排序 ( std::weak_ptr<T> 的 public 成员函数) |