operator==, !=, <, <=, >, >=, <=> (std::shared_ptr)
| ヘッダ <memory> で定義 |
||
| 2つの 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以降) |
2つのshared_ptr<T>オブジェクトを比較するか、shared_ptr<T>とヌルポインタを比較します。
shared_ptrの比較演算子は、単にポインタ値を比較するだけであり、実際に対象となっているオブジェクトは比較されません。shared_ptrにoperator<が定義されていることにより、shared_ptrをstd::mapやstd::setのような連想コンテナのキーとして使用できます。
|
|
(C++20以降) |
目次 |
[編集] パラメータ
| lhs | - | 比較対象の左辺のshared_ptr |
| rhs | - | 比較対象の右辺のshared_ptr |
[編集] 戻り値
[編集] 注釈
すべてのケースにおいて、比較されるのは格納されているポインタ(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 メンバー関数) |