名稱空間
變體
操作

std::owner_less

來自 cppreference.com
< cpp‎ | 記憶體
 
 
記憶體管理庫
(僅作說明*)
未初始化記憶體演算法
(C++17)
(C++17)
(C++17)
受約束的未初始化
記憶體演算法
C 庫

分配器
記憶體資源
垃圾回收支援
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化儲存
(直到 C++20*)
(直到 C++20*)
顯式生命週期管理
 
定義於標頭檔案 <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_ptrstd::shared_ptr。此排序使得僅當兩個智慧指標都為空或它們共享所有權時,才將它們比較為等價,即使透過 get() 獲取的原始指標值不同(例如,因為它們指向同一物件中的不同子物件)。

1)std::shared_ptrstd::weak_ptr 外,不為其他型別提供基於所有權的混合型別排序。
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)。

目錄

特化

當未指定 T 時,標準庫提供了 std::owner_less 的特化。在這種情況下,引數型別從實參推導(每個實參仍必須是 std::shared_ptrstd::weak_ptr)。

函式物件,提供共享指標和弱指標的混合型別所有權排序,無論被指向物件的型別如何
(類模板特化) [編輯]
(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()

僅為特化 (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 起)

使用基於所有權的語義比較 lhsrhs。等效於呼叫 lhs.owner_before(rhs)

此排序是嚴格弱排序關係。

lhsrhs 僅當它們都為空或共享所有權時才等價。

引數

lhs, rhs - 要比較的共享所有權指標

返回值

lhs 根據基於所有權的排序“小於”rhs 則為 true,否則為 false

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2873 C++11 operator() 未要求為 noexcept 要求為 noexcept

[編輯] 另請參閱

提供共享指標的基於所有權的排序
(std::shared_ptr<T> 的 public 成員函式) [編輯]
提供弱指標的基於所有權的排序
(std::weak_ptr<T> 的 public 成員函式) [編輯]