名前空間
変種
操作

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

From cppreference.com
< 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> で定義
2つの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_ptroperator<が定義されていることにより、shared_ptrstd::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では、2つのポインタが異なる場合があります。

[編集]

#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) は不正な形式でした。 定義が修正されました。

[編集] 関連項目

格納されたポインターを返す
(public メンバー関数) [編集]
English 日本語 中文(简体) 中文(繁體)