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以降) |
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 がゼロになったときにデリーターに渡されるもの)ではありません。これらの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)
が不適格であった
|
定義を修正 |
関連項目
|
格納されたポインタを返す
(公開メンバ関数) |