名前空間
変種
操作

std::owner_less

提供: cppreference.com
< cpp‎ | memory
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ(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++20)
(C++11)
(C++11)
C のライブラリ
低水準のメモリ管理
 
ヘッダ <memory> で定義
(1)
template<class T >
struct owner_less;/* undefined */
(C++11以上)
(C++17未満)
template<class T =void>
struct owner_less;/* undefined */
(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_ptrstd::shared_ptr 両方の型混合のオーナーベース (値ベースではなく) の順序付けを提供します。 順序は2つのスマートポインタがどちらも空の場合または所有権を共有している (例えば同じオブジェクト内の異なる部分オブジェクトを指しているなどにより、 get() によって取得される生のポインタ値が異なっていても) 場合にのみ同等であるようになります。

このクラステンプレートは

std::map<std::shared_ptr<T>, U, std::owner_less<std::shared_ptr<T>>>

std::map<std::weak_ptr<T>, U, std::owner_less<std::weak_ptr<T>>>

のように std::shared_ptr または std::weak_ptr をキーとして使用する連想コンテナを作るときに推奨される比較述語です。

デフォルトの operator< はウィークポインタに対しては定義されていませんし、同じオブジェクトに対する2つのシェアードポインタを間違って同等でないとみなす場合があります (shared_ptr::owner_before を参照してください)。

目次

[編集]特殊化

標準ライブラリは T が指定されないときの std::owner_less の特殊化を提供します。 この場合、引数の型は実引数から推定されます (その場合でも各引数は std::shared_ptr または std::weak_ptr のいずれかでなければなりません)。

指す先の型に関わらずシェアードポインタおよびウィークポインタの型混合のオーナーベースの順序付けを提供する関数オブジェクト
(クラステンプレートの特殊化)[edit]
(C++17以上)

メンバ型

メンバ型 定義
result_type(C++17で非推奨) 2-3) bool
first_argument_type(C++17で非推奨) 2) std::shared_ptr<T>
3) std::weak_ptr<T>
second_argument_type(C++17で非推奨) 2) std::shared_ptr<T>
3) std::weak_ptr<T>
(C++20未満)

[編集]メンバ関数

operator()
オーナーベースのセマンティクスを用いて引数を比較します
(関数)

std::owner_less::operator()

owner_less<shared_ptr<T>> テンプレート特殊化のみのメンバ
bool operator()(conststd::shared_ptr<T>& lhs,
                 conststd::shared_ptr<T>& rhs )constnoexcept;
(C++11以上)
owner_less<weak_ptr<T>> テンプレート特殊化のみのメンバ
bool operator()(conststd::weak_ptr<T>& lhs,
                 conststd::weak_ptr<T>& rhs )constnoexcept;
(C++11以上)
両方のテンプレート特殊化のメンバ
bool operator()(conststd::shared_ptr<T>& lhs,
                 conststd::weak_ptr<T>& rhs )constnoexcept;
(C++11以上)
bool operator()(conststd::weak_ptr<T>& lhs,
                 conststd::shared_ptr<T>& rhs )constnoexcept;
(C++11以上)

オーナーベースのセマンティクスを用いて lhsrhs を比較します。 実質的に lhs.owner_before(rhs) を呼びます。

順序は狭義弱順序関係です。

lhsrhs はどちらも空であるか所有権を共有する場合にのみ同等です。

引数

lhs, rhs - 比較する共有所有権のポインタ

戻り値

オーナーベースの順序付けによって決定されるところによって lhsrhs より小さい場合は true

[編集]欠陥報告

以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。

DR 適用先 発行時の動作 正しい動作
LWG 2873 C++11 the operator()'s might not be declared noexcept declared noexcept

[編集]関連項目

shared_ptr のオーナーベースの順序付けを提供します
(std::shared_ptr<T>のパブリックメンバ関数)[edit]
weak_ptr のオーナーベースの順序付けを提供します
(std::weak_ptr<T>のパブリックメンバ関数)[edit]
close