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) 曾是格式錯誤的 |
定義已修復 |
[編輯] 另請參閱
返回儲存的指標 (公共成員函式) |