std::owner_equal - cppreference.com

From cppreference.com

structowner_equal;(since C++26)This function object provides owner-based (as opposed to value-based) mixed-type equal comparison of both

std::weak_ptr

and

std::shared_ptr

. The comparison is such that two smart pointers compare equivalent only if they are both empty or if they share ownership, even if the values of the raw pointers obtained by get() are different (e.g. because they point at different subobjects within the same object).

It is the preferred comparison predicate when building unordered associative containers with

std::shared_ptr

and

std::weak_ptr

as keys together with std::owner_hash, that is, std::unordered_map<std::shared_ptr<T>,U,std::owner_hash,std::owner_equal> or std::unordered_map<std::weak_ptr<T>,U,std::owner_hash,std::owner_equal>.

Nested types

Member functions

compares its arguments using owner-based semantics
(function)std::owner_equal::operator()

template<classT,classU>constexprbooloperator()(conststd::shared_ptr<T>&lhs,conststd::shared_ptr<U>&rhs)constnoexcept; (1) (since C++26)template<classT,classU>constexprbooloperator()(conststd::shared_ptr<T>&lhs,conststd::weak_ptr<U>&rhs)constnoexcept; (2) (since C++26)template<classT,classU>constexprbooloperator()(conststd::weak_ptr<T>&lhs,conststd::shared_ptr<U>&rhs)constnoexcept; (3) (since C++26)template<classT,classU>constexprbooloperator()(conststd::weak_ptr<T>&lhs,conststd::weak_ptr<U>&rhs)constnoexcept; (4) (since C++26)Compares lhs and rhs using owner-based semantics. Effectively calls lhs.owner_equal(rhs).

The equal comparison is an equivalence relation.

lhs and rhs are equivalent only if they are both empty or share ownership.

Parameters

lhs, rhs - shared-ownership pointers to compare Return value

true if lhs and rhs are both empty or share ownership as determined by the owner-based equal comparison, false otherwise.

Notes

Feature-test

macro ValueStdFeature

__cpp_lib_smart_ptr_owner_equality

202306L

(C++26)Enabling the use of std::shared_ptr and std::weak_ptr as keys in

unordered associative containers

__cpp_lib_constexpr_memory

202506L

(C++26)Constexpr std::owner_equalSee also

(C++26)

provides owner-based equal comparison of shared pointers
(public member function of std::shared_ptr<T>)

[edit]

(C++26)

provides owner-based equal comparison of weak pointers
(public member function of std::weak_ptr<T>)

[edit]