名稱空間
變體
操作

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

來自 cppreference.com
 
 
記憶體管理庫
(僅作說明*)
未初始化記憶體演算法
(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) (C++11 起)
template< class T, class U >

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

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

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

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

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

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

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

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

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

                 const std::shared_ptr<U>& rhs ) noexcept;
(6) (C++11 起)
(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) (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,

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

比較兩個 shared_ptr 物件,或者比較 shared_ptr 與空指標。

請注意,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() 返回的指標),而不是管理的指標(當 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) 曾是格式錯誤的 定義已修復

[編輯] 另請參閱

返回儲存的指標
(公共成員函式) [編輯]