Namespaces
Variants

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

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
HTMLタグ、属性、C++コード内のテキストは翻訳せず、元のフォーマットを保持しました。C++固有の用語も翻訳していません。日付表記のみを日本語の慣用表現に変換しました。
定義済みヘッダー <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以降)

2つの shared_ptr<T> オブジェクトを比較するか、 shared_ptr<T> とヌルポインタを比較します。

shared_ptr の比較演算子は単にポインタ値を比較することに注意してください。実際に指し示されるオブジェクトは 比較されません shared_ptr に対して operator< が定義されていることで、 shared_ptr std::map std::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 がゼロになったときにデリーターに渡されるもの)ではありません。これらの2つのポインタは、 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++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 3427 C++20 operator<=>(shared_ptr, nullptr_t) が不適格であった 定義を修正

関連項目

格納されたポインタを返す
(公開メンバ関数)