Based on the accepted answer to this question, one can use a specialization to std
to provide a hash function for a user defined type.
#include <unordered_set>
#include <stdint.h>
struct FooBar {
int i;
};
namespace std {
template <> struct hash<FooBar>
{
size_t operator()(const FooBar & x) const
{
return x.i;
}
};
}
int main(){
std::unordered_set<FooBar> foo(0);
}
However, the documentation seems to imply that a custom hash function can also be passed explicitly into the constructor, and I would like to use a named function for this hash function.
However, my current attempt suffers from compile errors.
#include <unordered_set>
#include <stdint.h>
struct FooBar {
int i;
};
const size_t hashFooBar(const FooBar& foo) {
return foo.i;
}
int main(){
std::unordered_set<FooBar> foo(0, hashFooBar);
}
What is the correct template magic and method signature to make this work?