我正在尝试为我的自定义类型专门编写哈希函数,该类型是一个模板键。
我参考了cppreference上的内容。
编译器报错:“C++标准中没有为此类型提供哈希函数”。我猜想我可能写错了。编译器是否支持这种模板呢?
namespace std {
template<typename SType, typename AType, typename PType>
struct MyKey {
const SType from;
const AType consume;
const PType pop;
};
template<typename SType, typename AType, typename PType>
struct hash<MyKey<SType, AType, PType>> {
size_t operator ()(MyKey const &key) {
std::hash<SType>()(key.from);
std::hash<AType>()(key.consume);
std::hash<PType>()(key.pop);
}
};
}
MyKey
,还有SType
、AType
和PType
(至少不是已知类型的typedef
)。完成后,MyKey
的哈希值不应该只调用成员变量的哈希函数,而应该返回一个hash_combine
值,其中包含这些成员变量的哈希值。已经有很多现有的问题解释了这一点。 - Tony Delroy