Reputation: 115
How can I use boost::hash_value<std::pair<>>
in std::unordered_set<std::pair<>>
without defining a functor?
The following doesn't work:
typedef std::pair<unsigned, unsigned> S;
typedef size_t (hash_value_sample)(const S&);
std::function<hash_value_sample> hasher =
static_cast<hash_value_sample&>(boost::hash_value<unsigned, unsigned>);
std::unordered_set<S, decltype(hasher)> set;
set.insert(S(10, 10));
The execution aborts.
terminate called after throwing an instance of 'std::bad_function_call'
what(): bad_function_call
Abort
I think the above doesn't work because hasher() doesn't exist. I am using g++ -std=c++14
Upvotes: 0
Views: 772
Reputation: 6632
This seems to work, according to my tests:
#include <iostream>
#include <unordered_set>
#include <boost/functional/hash.hpp>
typedef std::pair<unsigned, unsigned> S;
typedef boost::hash<S> hash_t;
int main() {
std::unordered_set<S, hash_t> set;
set.insert(S(10, 10));
std::cout << set.count(S(10, 10)) << std::endl;
std::cout << set.count(S(10, 9)) << std::endl;
return 0;
}
Output:
❯ clang++ test.cpp -o test && ./test
1
0
Upvotes: 3