我正在为我的对象编写一个哈希函数。由于所有 STL-containers 的 Generic Hash 函数,我已经可以散列容器并组合散列。但我的课程也有枚举。当然,我可以为每个枚举创建一个哈希函数,但这似乎不是一个好主意。是否可以创建一些通用规范std::hash
,以便将其应用于每个枚举?类似的东西,使用std::enable_if
和std::is_enum
namespace std {
template <class E>
class hash<typename std::enable_if<std::is_enum<E>::value, E>::type> {
public:
size_t operator()( const E& e ) const {
return std::hash<std::underlying_type<E>::type>()( e );
}
};
};
PS。此代码无法编译
error: template parameters not used in partial specialization:
error: ‘E’