std::hash - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::hash

From cppreference.com
< cpp‎ | memory‎ | unique ptr

template<class T, class Deleter> struct hash<unique_ptr<T, Deleter>>; |  | (since C++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>.

For a given std::unique_ptr<T, Deleter> p, this specialization ensures that std::hash<std::unique_ptr<T, Deleter>>()(p) == std::hash<T*>()(p.get()).

### [edit] Example

Run this code

    #include <iostream>
    #include <memory>
    #include <functional>

    struct Foo {
        Foo() { std::cout << "Foo...\n"; }
        ~Foo() { std::cout << "~Foo...\n\n"; }
    };

    int main()
    {
        Foo* foo = new Foo();
        std::unique_ptr<Foo> up(foo);

        std::cout << "hash(up):  " << std::hash<std::unique_ptr<Foo>>()(up) << '\n';
        std::cout << "hash(foo): " << std::hash<Foo*>()(foo) << '\n';
    }

Output:

    Foo...
    hash(up):  3686401041
    hash(foo): 3686401041
    ~Foo...

### [edit] See also

hash (C++11) | hash function object (class template) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/memory/unique_ptr/hash&oldid=62793"