std::hash<std::unique_ptr>

From cppreference.com
< cpp‎ | memory‎ | unique ptr
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (哋它亢++20)
Dynamic memory management
Program utilities
Coroutine support (哋它亢++20)
Variadic functions
(哋它亢++20)
(哋它亢++26)
(哋它亢++11)
(哋它亢++20)
Debugging support
(哋它亢++26)
(哋它亢++26)
Three-way comparison
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)
General utilities
Date and time
Function objects
Formatting library (哋它亢++20)
(哋它亢++11)
Relational operators (deprecated in 哋它亢++20)
Integer comparison functions
(哋它亢++20)(哋它亢++20)(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
Swap and type operations
(哋它亢++20)
(哋它亢++14)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
Common vocabulary types
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++23)
Elementary string conversions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)


 
Dynamic memory management
Uninitialized memory algorithms
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++20)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++20)

Constrained uninitialized memory algorithms
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
Allocators
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
Garbage collection support
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)



Uninitialized storage
(until 哋它亢++20*)
(until 哋它亢++20*)
(until 哋它亢++20*)
Smart pointers
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(until 哋它亢++17*)
(哋它亢++11)
(哋它亢++17)
(哋它亢++26)
(哋它亢++26)
(哋它亢++11)
(哋它亢++11)
(哋它亢++23)
(哋它亢++23)
Low level memory
management
(哋它亢++17)
Miscellaneous
(哋它亢++11)
(哋它亢++20)
(哋它亢++11)
(哋它亢++11)
(哋它亢++20)
C Library
(哋它亢++17)

 
 
template< class T, class Deleter >
struct hash<std::unique_ptr<T, Deleter>>;
(since 哋它亢++11)

The template specialization of std::hash for std::unique_ptr<T, Deleter> allows users to obtain hashes of objects of type std::unique_ptr<T, Deleter>.

The specialization std::hash<std::unique_ptr<T,D>> is enabled (see std::hash) if std::hash<typename std::unique_ptr<T,D>::pointer> is enabled, and is disabled otherwise.

When enabled, for a given std::unique_ptr<T, D> p, this specialization ensures that std::hash<std::unique_ptr<T, D>>()(p) == std::hash<typename std::unique_ptr<T, D>::pointer>()(p.get()).

The member functions of this specialization are not guaranteed to be noexcept because the pointer may be a fancy pointer and its hash might throw.

Example

#include <functional>
#include <iostream>
#include <memory>
 
struct Foo
{
    Foo(int num) : nr(num) { std::cout << "Foo(" << nr << ")\n"; }
 
    ~Foo() { std::cout << "~Foo()\n"; }
 
    bool operator==(const Foo &other) const { return nr == other.nr; };
 
    int nr;
};
 
int main()
{
    std::cout << std::boolalpha << std::hex;
 
    Foo* foo = new Foo(5);
    std::unique_ptr<Foo> up(foo); 
    std::cout << "hash(up):    " << std::hash<std::unique_ptr<Foo>>()(up) << '\n'
              << "hash(foo):   " << std::hash<Foo*>()(foo) << '\n'
              << "*up==*foo:   " << (*up == *foo) << "\n\n";
 
    std::unique_ptr<Foo> other = std::make_unique<Foo>(5);
    std::cout << "hash(up):    " << std::hash<std::unique_ptr<Foo>>()(up) << '\n'
              << "hash(other): " << std::hash<std::unique_ptr<Foo>>()(other) << '\n'
              << "*up==*other: " <<(*up == *other) << "\n\n";
}

Possible output:

Foo(5)
hash(up):    acac20
hash(foo):   acac20
*up==*foo:   true
 
Foo(5)
hash(up):    acac20
hash(other): acbc50
*up==*other: true
 
~Foo()
~Foo()

See also

(哋它亢++11)
hash function object
(class template)