operator==, !=, <, <=, >, >=, <=> (std::shared_ptr)

来自cppreference.com
< cpp‎ | memory‎ | shared ptr


 
 
内存管理库
(仅用于阐述*)
分配器
未初始化内存算法
受约束的未初始化内存算法
内存资源
未初始化存储(C++20 前)
(C++17 弃用)
(C++17 弃用)
垃圾收集器支持(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
 
 
在标头 <memory> 定义
比较两个 shared_ptr 对象
template<class T, class U >

bool operator==(conststd::shared_ptr<T>& lhs,

                 conststd::shared_ptr<U>& rhs )noexcept;
(1) (C++11 起)
template<class T, class U >

bool operator!=(conststd::shared_ptr<T>& lhs,

                 conststd::shared_ptr<U>& rhs )noexcept;
(2) (C++11 起)
(C++20 前)
template<class T, class U >

bool operator<(conststd::shared_ptr<T>& lhs,

                conststd::shared_ptr<U>& rhs )noexcept;
(3) (C++11 起)
(C++20 前)
template<class T, class U >

bool operator>(conststd::shared_ptr<T>& lhs,

                conststd::shared_ptr<U>& rhs )noexcept;
(4) (C++11 起)
(C++20 前)
template<class T, class U >

bool operator<=(conststd::shared_ptr<T>& lhs,

                 conststd::shared_ptr<U>& rhs )noexcept;
(5) (C++11 起)
(C++20 前)
template<class T, class U >

bool operator>=(conststd::shared_ptr<T>& lhs,

                 conststd::shared_ptr<U>& rhs )noexcept;
(6) (C++11 起)
(C++20 前)
template<class T, class U >

std::strong_ordering operator<=>(conststd::shared_ptr<T>& lhs,

                                  conststd::shared_ptr<U>& rhs )noexcept;
(7) (C++20 起)
比较 shared_ptr 与空指针
template<class T >
bool operator==(conststd::shared_ptr<T>& lhs, std::nullptr_t)noexcept;
(8) (C++11 起)
template<class T >
bool operator==(std::nullptr_t, conststd::shared_ptr<T>& rhs )noexcept;
(9) (C++11 起)
(C++20 前)
template<class T >
bool operator!=(conststd::shared_ptr<T>& lhs, std::nullptr_t)noexcept;
(10) (C++11 起)
(C++20 前)
template<class T >
bool operator!=(std::nullptr_t, conststd::shared_ptr<T>& rhs )noexcept;
(11) (C++11 起)
(C++20 前)
template<class T >
bool operator<(conststd::shared_ptr<T>& lhs, std::nullptr_t)noexcept;
(12) (C++11 起)
(C++20 前)
template<class T >
bool operator<(std::nullptr_t, conststd::shared_ptr<T>& rhs )noexcept;
(13) (C++11 起)
(C++20 前)
template<class T >
bool operator>(conststd::shared_ptr<T>& lhs, std::nullptr_t)noexcept;
(14) (C++11 起)
(C++20 前)
template<class T >
bool operator>(std::nullptr_t, conststd::shared_ptr<T>& rhs )noexcept;
(15) (C++11 起)
(C++20 前)
template<class T >
bool operator<=(conststd::shared_ptr<T>& lhs, std::nullptr_t)noexcept;
(16) (C++11 起)
(C++20 前)
template<class T >
bool operator<=(std::nullptr_t, conststd::shared_ptr<T>& rhs )noexcept;
(17) (C++11 起)
(C++20 前)
template<class T >
bool operator>=(conststd::shared_ptr<T>& lhs, std::nullptr_t)noexcept;
(18) (C++11 起)
(C++20 前)
template<class T >
bool operator>=(std::nullptr_t, conststd::shared_ptr<T>& rhs )noexcept;
(19) (C++11 起)
(C++20 前)
template<class T >

std::strong_ordering operator<=>(conststd::shared_ptr<T>& lhs,

                                  std::nullptr_t)noexcept;
(20) (C++20 起)

比较两个 shared_ptr<T> 对象或将 shared_ptr<T> 与空指针比较。

注意 shared_ptr 的比较运算符单纯比较指针值;而比较所指向的实际对象。为 shared_ptr 定义 operator< 允许将 shared_ptr 用作关联容器(如 std::mapstd::set)中的键。

<<=>>=!= 运算符分别从 operator<=>operator==合成

(C++20 起)

目录

[编辑]参数

lhs - 要比较的左侧 shared_ptr
rhs - 要比较的右侧 shared_ptr

[编辑]返回值

1)lhs.get() == rhs.get()
2)!(lhs == rhs)
3)std::less<V>()(lhs.get(), rhs.get()),其中 V 是 std::shared_ptr<T>::element_type*std::shared_ptr<U>::element_type*合成指针类型
4)rhs < lhs
5)!(rhs < lhs)
6)!(lhs < rhs)
7)std::compare_three_way{}(x.get(), y.get())
8)!lhs
9)!rhs
10)(bool)lhs
11)(bool)rhs
12)std::less<std::shared_ptr<T>::element_type*>()(lhs.get(), nullptr)
13)std::less<std::shared_ptr<T>::element_type*>()(nullptr, rhs.get())
14)nullptr < lhs
15)rhs < nullptr
16)!(nullptr < lhs)
17)!(rhs < nullptr)
18)!(lhs < nullptr)
19)!(nullptr < rhs)
20)std::compare_three_way{}(x.get(), static_cast<std::shared_ptr<T>::element_type*>(nullptr))

[编辑]注解

所有情况下,被比较者是存储的指针(get() 所返回者)而非被管理的指针(uses_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'// C++20 起   // p1 和 p2 指向不同的内存地址, 因此 p1 != p2<<"(p1 == p2)  : "<<(p1 == p2)<<'\n'<<"(p1 < p2)  : "<<(p1 < p2)<<'\n'<<"(p1 <=> p2) < 0  : "<<((p1 <=> p2)<0)<<'\n'// C++20 起<<"(p1 <=> p2) == 0 : "<<((p1 <=> p2)==0)<<'\n';// 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) 非良构 修正定义

[编辑]参阅

返回存储的指针
(公开成员函数)[编辑]
close