std::owner_less
來自 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。此排序使得僅當兩個智慧指標都為空或它們共享所有權時,才將它們比較為等價,即使透過 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< 未為弱指標定義,並且可能錯誤地將指向同一物件的兩個共享指標視為不等價(參見 std::shared_ptr::owner_before)。
特化當未指定
|
(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++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2873 | C++11 | operator() 未要求為 noexcept | 要求為 noexcept |
[編輯] 另請參閱
提供共享指標的基於所有權的排序 ( std::shared_ptr<T> 的 public 成員函式) | |
提供弱指標的基於所有權的排序 ( std::weak_ptr<T> 的 public 成員函式) |