std::shared_ptr<T>:: owner_before
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Modifiers | ||||
| Observers | ||||
|
(C++17)
|
||||
|
(
until C++20*
)
|
||||
|
shared_ptr::owner_before
|
||||
|
(C++26)
|
||||
|
(C++26)
|
||||
| Non-member functions | ||||
|
(until C++20)
(until C++20)
(until C++20)
(until C++20)
(until C++20)
(C++20)
|
||||
|
functions
(
until C++26*
)
|
||||
| Helper classes | ||||
|
(C++20)
|
||||
| Deduction guides (C++17) |
|
template
<
class
Y
>
bool owner_before ( const shared_ptr < Y > & other ) const noexcept ; |
||
|
template
<
class
Y
>
bool owner_before ( const std:: weak_ptr < Y > & other ) const noexcept ; |
||
この
shared_ptr
が、実装定義の所有者ベース(値ベースとは対照的に)順序において
other
より先行するかどうかをチェックします。この順序は、両方が空であるか、または両方が同じオブジェクトを所有している場合にのみ、二つのスマートポインタが等価と比較されるような順序です(例:同じオブジェクト内の異なる部分オブジェクトを指しているために
get()
によって取得されたポインタの値が異なる場合でも)。
この順序付けは、共有ポインタと弱ポインタを連想コンテナのキーとして使用できるようにするために用いられます。通常は std::owner_less を通じて実現されます。
目次 |
パラメータ
| other | - | 比較対象の std::shared_ptr または std::weak_ptr |
戻り値
true が * this が other に先行する場合、 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' // コンパイル不可 // << "w3 < w2 " << (w3 < w2) << '\n' // コンパイル不可 << "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 2873 | C++11 |
owner_before
が宣言されていなかった
noexcept
|
宣言された noexcept |
関連項目
|
(C++11)
|
sharedポインタとweakポインタの混合型所有者ベースの順序付けを提供する
(クラステンプレート) |