名前空間
変種
操作

std::shared_ptr<T>::owner_before

提供: cppreference.com
< cpp‎ | memory‎ | shared ptr
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
動的メモリ管理
スマートポインタ
(C++11)
(C++11)
(C++11)
(C++17未満)
(C++11)
アロケータ
メモリリソース
未初期化記憶��
ガベージコレクションサポート
その他
(C++20)
(C++11)
(C++11)
C のライブラリ
低水準のメモリ管理
 
 
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_ptrother より前に来るかどうかを調べます。 この順序は、2つのスマートポインタがどちらも空であるか、同じオブジェクトを所有していれば、 get() によって取得されるポインタの値が異なっていても (例えば同じオブジェクトの別々のサブオブジェクトを指しているなど)、それらは等しいものとして比較されます。

この順序付けは、 shared_ptr や weak_ptr を連想コンテナのキーとして使用可能とするために、一般的には std::owner_less を通して、使用されます。

目次

[編集] 引数

other - 比較する std::shared_ptr または std::weak_ptr

[編集] 戻り値

*thisother より前に来るならば 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 2873 C++11 owner_before might not be declared noexcept declared noexcept

[編集] 関連項目

shared_ptr と weak_ptr の混ざった型に対するオーナーベースの順序付けを提供します
(クラステンプレート) [edit]