Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

hash is deleted for std::pair? #140

Open
h-2 opened this issue Jan 5, 2022 · 3 comments
Open

hash is deleted for std::pair? #140

h-2 opened this issue Jan 5, 2022 · 3 comments

Comments

@h-2
Copy link

h-2 commented Jan 5, 2022

And I have another one. Simply declaring this flat_map already fails:

robin_hood::unordered_flat_map<std::pair<uint16_t, uint16_t>, std::vector<std::pair<uint16_t, uint16_t>>> foo;

Godbolt link: https://godbolt.org/z/Ezca6K1xn

@capitalai
Copy link

You can change 2 lines and it may works.

struct hash: public std::hash<T> { => 'struct hash {
auto result = std::hash<T>::operator()(obj); => auto result = std::hash<T>{}(obj);

@slavenf
Copy link

slavenf commented Jan 19, 2022

@h-2 The specialization of std::hash for std::pair<T1, T2> does not exist in standard C++ library. You have to write it yourself. You can see that note on https://en.cppreference.com/w/cpp/utility/hash

@slavenf
Copy link

slavenf commented Jan 19, 2022

Here is an example how to write specialization of std::hash for std::pair:

// This is similar to boost::hash_combine
//
template <class T>
void hash_combine(std::size_t& seed, const T& v)
{
    std::hash<T> hasher;
    seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

namespace std
{

template <class T1, class T2>
struct hash<std::pair<T1, T2>>
{
    std::size_t operator()(const std::pair<T1, T2>& p) const noexcept
    {
        std::size_t seed = 0;

        hash_combine(seed, p.first);
        hash_combine(seed, p.second);

        return seed;
    }
};

}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants