std::owner_equal
來自 cppreference.com
定義於標頭檔案 <memory> |
||
struct owner_equal; |
(C++26 起) | |
此函式物件提供基於所有權(而非基於值)的 std::weak_ptr 和 std::shared_ptr 混合型別相等比較。這種比較使得兩個智慧指標只有在它們都為空,或者它們共享所有權時才被認為是等價的,即使透過 get()
獲得的原始指標值不同(例如,因為它們指向同一物件內不同的子物件)。
當使用 std::shared_ptr 和 std::weak_ptr 作為鍵,並結合 std::owner_hash 構建無序關聯容器時,它是首選的比較謂詞,例如 std::unordered_map<std::shared_ptr<T>, U, std::owner_hash, std::owner_equal> 或 std::unordered_map<std::weak_ptr<T>, U, std::owner_hash, std::owner_equal>。
3)
std::owner_equal
從引數中推導引數型別。目錄 |
[edit] 巢狀型別
巢狀型別 | 定義 |
is_transparent
|
未指定 |
[edit] 成員函式
operator() |
使用基於所有權的語義比較其引數 (函式) |
std::owner_equal::operator()
template< class T, class U > bool operator()( const std::shared_ptr<T>& lhs, |
(C++26 起) | |
template< class T, class U > bool operator()( const std::shared_ptr<T>& lhs, |
(C++26 起) | |
template< class T, class U > bool operator()( const std::weak_ptr<T>& lhs, |
(C++26 起) | |
template< class T, class U > bool operator()( const std::weak_ptr<T>& lhs, |
(C++26 起) | |
使用基於所有權的語義比較 lhs 和 rhs。實際上呼叫 lhs.owner_equal(rhs)。
相等比較是一種等價關係。
lhs 和 rhs 僅當它們都為空或共享所有權時才等價。
引數
lhs, rhs | - | 要比較的共享所有權指標 |
返回值
true 如果 lhs 和 rhs 都為空或透過基於所有權的相等比較確定共享所有權,否則為 false。
[edit] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_smart_ptr_owner_equality |
202306L |
(C++26) | 在無序關聯容器中啟用將 std::shared_ptr 和 std::weak_ptr 用作鍵 |
[edit] 參閱
(C++26) |
提供共享指標的基於所有權的相等比較 ( std::shared_ptr<T> 的公共成員函式) |
(C++26) |
提供弱指標的基於所有權的相等比較 ( std::weak_ptr<T> 的公共成員函式) |