std::owner_equal(3)

πŸ“¦
version 2024.07.05 Β· 2 versions
, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::owner_equal πŸ’–

okay bestie, welcome to the std::owner_equal(3) page from the C++ Standard Libary (yep, that's the header spelling β€” we're keeping the detail, not correcting it πŸ’…). This is the part where C++ gets all β€œwho actually owns this?” and honestly it ate.

πŸŽ€ NAME πŸŽ€

std::owner_equal - std::owner_equal. That's the name and the one-line summary, no notes. Iconic.

🌈 Synopsis 🌈

Defined in header <memory>, and the declaration is literally struct owner_equal; β€” (since C++26), bestie.

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).

A few receipts:

  1. Owner-based mixed-type equal comparison is not provided for types other than std::shared_ptr and std::weak_ptr.
  2. The owner-based mixed-type equal comparison of std::shared_ptr and std::weak_ptr. 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>.
  3. std::owner_equal deduces the parameter types from the arguments.

✨ Member functions ✨

  • operator() β€” compares its arguments using owner-based semantics (function). Yup, that's the whole mood.

πŸ”₯ std::owner_equal::operator() πŸ”₯

Every overload below is (since C++26):

template< class T, class U >
bool operator()( const std::shared_ptr<T>& lhs,
                 const std::shared_ptr<U>& rhs ) const noexcept;

template< class T, class U >
bool operator()( const std::shared_ptr<T>& lhs,
                 const std::weak_ptr<U>& rhs ) const noexcept;

template< class T, class U >
bool operator()( const std::weak_ptr<T>& lhs,
                 const std::shared_ptr<U>& rhs ) const noexcept;

template< class T, class U >
bool operator()( const std::weak_ptr<T>& lhs,
                 const std::weak_ptr<U>& rhs ) const noexcept;

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: __cpp_lib_smart_ptr_owner_equality
  • Value: 202306L
  • Std: (C++26)
  • Feature: Enabling the use of std::shared_ptr and std::weak_ptr as keys in unordered associative containers.

🌸 See also 🌸

  • owner_equal provides owner-based equal comparison of shared pointers (C++26) (public member function of std::shared_ptr<T>).
  • owner_equal provides owner-based equal comparison of weak pointers (C++26) (public member function of std::weak_ptr<T>).

xoxo, your Unix bestie πŸ’… β€” source/version: http://cppreference.com, date: 2024.06.10.

πŸ“¦ where this page comes from