命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | memory‎ | shared ptr
 
 
内存管理库
(仅为说明目的*)
未初始化内存算法
(C++17)
(C++17)
(C++17)
约束的未初始化
内存算法
C 库

分配器
内存资源
垃圾回收支持
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化存储
(直到 C++20*)
(直到 C++20*)
显式生命周期管理
 
 
定义于头文件 <memory>
比较两个 shared_ptr 对象。
template< class T, class U >

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

                 const std::shared_ptr<U>& rhs ) noexcept;
(1) (since C++11)
template< class T, class U >

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

                 const std::shared_ptr<U>& rhs ) noexcept;
(2) (since C++11)
(until C++20)
template< class T, class U >

bool operator<( const std::shared_ptr<T>& lhs,

                const std::shared_ptr<U>& rhs ) noexcept;
(3) (since C++11)
(until C++20)
template< class T, class U >

bool operator>( const std::shared_ptr<T>& lhs,

                const std::shared_ptr<U>& rhs ) noexcept;
(4) (since C++11)
(until C++20)
template< class T, class U >

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

                 const std::shared_ptr<U>& rhs ) noexcept;
(5) (since C++11)
(until C++20)
template< class T, class U >

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

                 const std::shared_ptr<U>& rhs ) noexcept;
(6) (since C++11)
(until C++20)
template< class T, class U >

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

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

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

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

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

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

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

(since 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() 返回的指针),而不是托管的指针(当 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++ 标准。

DR 应用于 已发布行为 正确行为
LWG 3427 C++20 operator<=>(shared_ptr, nullptr_t) 格式错误 定义已修复

[编辑] 参见

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