std::owner_hash

From cppreference.com
< cpp‎ | memory
Memory management library
(exposition only*)
Uninitialized memory algorithms
(C++17)
(C++17)
(C++17)
(C++20)
Constrained uninitialized
memory algorithms
(C++20)
C Library

Allocators
(C++11)
(C++11)
Memory resources
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
Uninitialized storage
(until C++20*)
(until C++20*)
(until C++20*)
Explicit lifetime management
Defined in header <memory>
struct owner_hash;
(since C++26)

This function object provides owner-based (as opposed to value-based) hashing of both std::weak_ptr and std::shared_ptr

Nested types

Nested type Definition
is_transparent unspecified

Member functions

operator()
calculates the hash of the shared-ownership pointer
(function)

std::owner_hash::operator()

template < class T >
std::size_t operator( ) ( const std::shared_ptr <T> & key ) const noexcept ;
(1) (since C++26)
template < class T >
std::size_t operator( ) ( const std::weak_ptr <T> & key ) const noexcept ;
(2) (since C++26)

Equivalent to return key.owner_hash();.

Parameters

key - shared-ownership pointer to be hashed

Return value

A hash value that is identical for any std::shared_ptr or std::weak_ptr

Notes

Feature-test macro Value Std Feature
__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

See also

(C++26)
provides owner-based hashing of shared pointers
(public member function of std::shared_ptr<T>)
(C++26)
provides owner-based hashing of weak pointers
(public member function of std::weak_ptr<T>)