operator==, !=, <, <=, >, >=, <=> (std::shared_ptr)
| 定义于头文件  <memory> | ||
| 比较两个  shared_ptr对象。 | ||
| template< class T, class U > bool operator==( const std::shared_ptr<T>& lhs, | (1) | (C++11 起) | 
| template< class T, class U > bool operator!=( const std::shared_ptr<T>& lhs, | (2) | (C++11 起) (C++20 前) | 
| template< class T, class U > bool operator<( const std::shared_ptr<T>& lhs, | (3) | (C++11 起) (C++20 前) | 
| template< class T, class U > bool operator>( const std::shared_ptr<T>& lhs, | (4) | (C++11 起) (C++20 前) | 
| template< class T, class U > bool operator<=( const std::shared_ptr<T>& lhs, | (5) | (C++11 起) (C++20 前) | 
| template< class T, class U > bool operator>=( const std::shared_ptr<T>& lhs, | (6) | (C++11 起) (C++20 前) | 
| template< class T, class U > std::strong_ordering operator<=>( const std::shared_ptr<T>& lhs, | (7) | (C++20 起) | 
| 比较  shared_ptr与空指针。 | ||
| template< class T > bool operator==( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept; | (8) | (C++11 起) | 
| template< class T > bool operator==( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept; | (9) | (C++11 起) (C++20 前) | 
| template< class T > bool operator!=( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept; | (10) | (C++11 起) (C++20 前) | 
| template< class T > bool operator!=( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept; | (11) | (C++11 起) (C++20 前) | 
| template< class T > bool operator<( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept; | (12) | (C++11 起) (C++20 前) | 
| template< class T > bool operator<( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept; | (13) | (C++11 起) (C++20 前) | 
| template< class T > bool operator>( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept; | (14) | (C++11 起) (C++20 前) | 
| template< class T > bool operator>( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept; | (15) | (C++11 起) (C++20 前) | 
| template< class T > bool operator<=( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept; | (16) | (C++11 起) (C++20 前) | 
| template< class T > bool operator<=( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept; | (17) | (C++11 起) (C++20 前) | 
| template< class T > bool operator>=( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept; | (18) | (C++11 起) (C++20 前) | 
| template< class T > bool operator>=( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept; | (19) | (C++11 起) (C++20 前) | 
| template< class T > std::strong_ordering operator<=>( const std::shared_ptr<T>& lhs, | (20) | (C++20 起) | 
比较两个 shared_ptr 对象,或者比较 shared_ptr 与空指针。
请注意,shared_ptr 的比较运算符仅比较指针值;实际指向的对象**不**进行比较。为 shared_ptr 定义 operator< 允许 shared_ptr 作为关联容器(如 std::map 和 std::set)的键使用。
| 
 | (C++20 起) | 
| 目录 | 
[编辑] 参数
| lhs | - | 要比较的左侧 shared_ptr | 
| rhs | - | 要比较的右侧 shared_ptr | 
[编辑] 返回值
[编辑] 注意
在所有情况下,比较的是存储的指针(由 get() 返回的指针),而不是管理的指针(当 use_count 归零时传递给删除器的指针)。在使用别名构造函数创建的 shared_ptr 中,这两个指针可能不同。
[编辑] 示例
#include <iostream> #include <memory> int main() { std::shared_ptr<int> p1(new int(42)); std::shared_ptr<int> p2(new int(42)); std::cout << std::boolalpha << "(p1 == p1) : " << (p1 == p1) << '\n' << "(p1 <=> p1) == 0 : " << ((p1 <=> p1) == 0) << '\n' // Since C++20 // p1 and p2 point to different memory locations, so p1 != p2 << "(p1 == p2) : " << (p1 == p2) << '\n' << "(p1 < p2) : " << (p1 < p2) << '\n' << "(p1 <=> p2) < 0 : " << ((p1 <=> p2) < 0) << '\n' // Since C++20 << "(p1 <=> p2) == 0 : " << ((p1 <=> p2) == 0) << '\n'; // Since C++20 }
可能的输出
(p1 == p1) : true (p1 <=> p1) == 0 : true (p1 == p2) : false (p1 < p2) : true (p1 <=> p2) < 0 : true (p1 <=> p2) == 0 : false
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 | 
|---|---|---|---|
| LWG 3427 | C++20 | operator<=> (shared_ptr, nullptr_t)曾是格式错误的 | 定义已修复 | 
[编辑] 另请参阅
| 返回存储的指针 (公共成员函数) | 


