std::shared_ptr<T>::owner_equal
來自 cppreference.com
< cpp | 記憶體 | shared ptr
template< class Y > bool owner_equal( const std::shared_ptr<Y>& other ) const noexcept; |
(1) | (C++26 起) |
template< class Y > bool owner_equal( const std::weak_ptr<Y>& other ) const noexcept; |
(2) | (C++26 起) |
檢查此 shared_ptr
和 other 是否共享所有權,或者是否都為空。比較結果是,兩個智慧指標僅在它們都為空或它們都擁有同一物件時才比較相等,即使透過 get() 獲取的指標值不同(例如,因為它們指向同一物件內不同的子物件)。
成員函式 owner_equal
是一種等價關係,使得 !owner_before(other) && !other.owner_before(*this) 為 true 當且僅當 owner_equal(other) 為 true。
此排序用於使共享指標和弱指標可用作無序關聯容器中的鍵,通常透過 std::owner_equal。
目錄 |
[編輯] 引數
其他 | - | 要比較的 std::shared_ptr 或 std::weak_ptr |
[編輯] 返回值
如果 *this 和 other 共享所有權或都為空,則返回 true。否則,返回 false。
[編輯] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_smart_ptr_owner_equality |
202306L |
(C++26) | 允許在無序關聯容器中使用 std::shared_ptr 作為鍵 |
[編輯] 示例
本節不完整 原因:示例 |
[編輯] 另請參見
(C++26) |
提供基於所有者的共享指標和弱指標的混合型別相等比較 (類) |