std::weak_ptr<T>::owner_before
From cppreference.com
| template< class Y > bool owner_before( const weak_ptr<Y>& other ) const noexcept; |
||
| template< class Y > bool owner_before( const std::shared_ptr<Y>& other ) const noexcept; |
||
このweak_ptrが、実装定義の所有者ベース(値ベースではない)の順序でotherより前に来るかどうかをチェックします。順序は、2つのスマートポインタが空であるか、またはget()によって取得されたポインタの値が(例えば、同じオブジェクト内の異なるサブオブジェクトを指しているために)異なっていても、両方とも同じオブジェクトを所有している場合にのみ等価とみなされるように定義されています。
この順序付けは、std::owner_lessを通じて、共有ポインタと弱ポインタを連想コンテナのキーとして使用可能にするために使用されます。
目次 |
[編集] パラメータ
| その他 | - | 比較対象のstd::shared_ptrまたはstd::weak_ptr |
[編集] 戻り値
*thisがotherより前に来る場合はtrue、それ以外の場合はfalse。一般的な実装では、制御ブロックのアドレスを比較します。
[編集] 例
このコードを実行
#include <iostream> #include <memory> struct Foo { int n1; int n2; Foo(int a, int b) : n1(a), n2(b) {} }; int main() { auto p1 = std::make_shared<Foo>(1, 2); std::shared_ptr<int> p2(p1, &p1->n1); std::shared_ptr<int> p3(p1, &p1->n2); std::cout << std::boolalpha << "p2 < p3 " << (p2 < p3) << '\n' << "p3 < p2 " << (p3 < p2) << '\n' << "p2.owner_before(p3) " << p2.owner_before(p3) << '\n' << "p3.owner_before(p2) " << p3.owner_before(p2) << '\n'; std::weak_ptr<int> w2(p2); std::weak_ptr<int> w3(p3); std::cout // << "w2 < w3 " << (w2 < w3) << '\n' // won't compile // << "w3 < w2 " << (w3 < w2) << '\n' // won't compile << "w2.owner_before(w3) " << w2.owner_before(w3) << '\n' << "w3.owner_before(w2) " << w3.owner_before(w2) << '\n'; }
出力
p2 < p3 true p3 < p2 false p2.owner_before(p3) false p3.owner_before(p2) false w2.owner_before(w3) false w3.owner_before(w2) false
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2083 | C++11 | owner_beforeがconstとして宣言されていませんでした |
constとして宣言されました |
| LWG 2942 | C++11 | owner_beforeがnoexceptとして宣言されていませんでした |
noexcept として宣言された |
[編集] 関連項目
| (C++11) |
shared ポインタと weak ポインタの、所有者ベースの混合型の順序付けを提供します (クラステンプレート) |