std::owner_less
From cppreference.com
| ヘッダ <memory> で定義 |
||
| (1) | ||
| template< class T > struct owner_less; /* 未定義 */ |
(C++11以降) (C++17まで) |
|
| template< class T = void > struct owner_less; /* 未定義 */ |
(C++17以降) | |
| template< class T > struct owner_less<std::shared_ptr<T>>; |
(2) | (C++11以降) |
| template< class T > struct owner_less<std::weak_ptr<T>>; |
(3) | (C++11以降) |
| template<> struct owner_less<void>; |
(4) | (C++17以降) |
この関数オブジェクトは、std::weak_ptrとstd::shared_ptrの両方に対して、所有者ベースの(値ベースとは対照的な)混合型順序を提供します。この順序付けは、2つのスマートポインタが両方とも空であるか、または所有権を共有している場合にのみ等価であるとみなされます。これは、get()によって取得される生のポインタの値が異なる場合でも(例えば、同じオブジェクト内の異なるサブオブジェクトを指している場合でも)同様です。
2) std::shared_ptrの所有者ベースの混合型順序付け。
これは、std::shared_ptrをキーとして連想コンテナを構築する際の推奨される比較述語です。つまり、std::map<std::shared_ptr<T>, U, std::owner_less<std::shared_ptr<T>>>。
3) std::weak_ptrの所有者ベースの混合型順序付け。
これは、std::weak_ptrをキーとして連想コンテナを構築する際の推奨される比較述語です。つまり、std::map<std::weak_ptr<T>, U, std::owner_less<std::weak_ptr<T>>>。
4) void特殊化は、引数からパラメータ型を推論します。
デフォルトのoperator<はweak pointerには定義されておらず、同じオブジェクトに対する2つのshared pointerを誤って非等価と見なす可能性があります(std::shared_ptr::owner_beforeを参照)。
特殊化標準ライブラリは、`T`が指定されていない場合に`std::owner_less`の特殊化を提供します。この場合、パラメータの型は引数から推論されます(それぞれの引数は引き続きstd::shared_ptrまたはstd::weak_ptrのいずれかでなければなりません)。
|
(C++17以降) |
ネストされた型
|
(C++20まで) |
[編集] メンバ関数
| operator() |
引数を所有者ベースのセマンティクスで比較する (関数) |
std::owner_less::operator()
| 特殊化 (2) のみのメンバ |
||
| bool operator()( const std::shared_ptr<T>& lhs, const std::shared_ptr<T>& rhs ) const noexcept; |
(C++11以降) | |
| 特殊化 (3) のみのメンバ |
||
| bool operator()( const std::weak_ptr<T>& lhs, const std::weak_ptr<T>& rhs ) const noexcept; |
(C++11以降) | |
| 両方のテンプレート特殊化のメンバ |
||
| bool operator()( const std::shared_ptr<T>& lhs, const std::weak_ptr<T>& rhs ) const noexcept; |
(C++11以降) | |
| bool operator()( const std::weak_ptr<T>& lhs, const std::shared_ptr<T>& rhs ) const noexcept; |
(C++11以降) | |
lhs と rhs を所有者ベースのセマンティクスで比較します。実質的に lhs.owner_before(rhs) を呼び出します。
この順序は厳密弱順序関係です。
lhs と rhs は、両方が空であるか、または所有権を共有している場合にのみ等価です。
パラメータ
| lhs, rhs | - | 比較する共有所有権ポインタ |
戻り値
lhs が所有者ベースの順序付けによって rhs より小さいと判断された場合は true、それ以外の場合は false。
[編集] 欠陥報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2873 | C++11 | operator()はnoexceptである必要はなかった | noexceptであることが要求される |
[編集] 関連項目
| 共有ポインタの所有者ベースの順序を提供します ( std::shared_ptr<T> のパブリックメンバ関数) | |
| weak pointer の所有者ベースの順序付けを提供します ( std::weak_ptr<T> のパブリックメンバ関数) |