名稱空間
變體
操作

std::owner_equal

來自 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>
struct owner_equal;
(C++26 起)

此函式物件提供基於所有權(而非基於值)的 std::weak_ptrstd::shared_ptr 混合型別相等比較。這種比較使得兩個智慧指標只有在它們都為空,或者它們共享所有權時才被認為是等價的,即使透過 get() 獲得的原始指標值不同(例如,因為它們指向同一物件內不同的子物件)。

1) 除了 std::shared_ptrstd::weak_ptr 之外的型別不提供基於所有權的混合型別相等比較。
2) std::shared_ptrstd::weak_ptr 的基於所有權的混合型別相等比較。
當使用 std::shared_ptrstd::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,

                 const std::shared_ptr<U>& rhs ) const noexcept;
(C++26 起)
template< class T, class U >

bool operator()( const std::shared_ptr<T>& lhs,

                 const std::weak_ptr<U>& rhs ) const noexcept;
(C++26 起)
template< class T, class U >

bool operator()( const std::weak_ptr<T>& lhs,

                 const std::shared_ptr<U>& rhs ) const noexcept;
(C++26 起)
template< class T, class U >

bool operator()( const std::weak_ptr<T>& lhs,

                 const std::weak_ptr<U>& rhs ) const noexcept;
(C++26 起)

使用基於所有權的語義比較 lhsrhs。實際上呼叫 lhs.owner_equal(rhs)

相等比較是一種等價關係。

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

引數

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

返回值

true 如果 lhsrhs 都為空或透過基於所有權的相等比較確定共享所有權,否則為 false

[edit] 注意

特性測試 標準 特性
__cpp_lib_smart_ptr_owner_equality 202306L (C++26) 無序關聯容器中啟用將 std::shared_ptrstd::weak_ptr 用作鍵

[edit] 參閱

提供共享指標的基於所有權的相等比較
(std::shared_ptr<T> 的公共成員函式) [編輯]
提供弱指標的基於所有權的相等比較
(std::weak_ptr<T> 的公共成員函式) [編輯]