【发布时间】:2021-01-01 07:53:08
【问题描述】:
为什么 C++11 需要哈希模板特化来定义 result_type 和 argument_type?我发现了一些旧代码,其中用户定义的类型成功用于 unordered_map<> 键,但没有这两个 typedef。
namespace std {
template<> struct hash<MyNamespace::Foo> {
typedef size_t result_type;
typedef MyNamespace::Foo argument_type;
size_t operator() (const MyNamespace::Foo& f ) const { return (size_t) f.u32InternalValue; }
};
}
【问题讨论】:
-
@cigien 例如en.cppreference.com/w/cpp/utility/hash 具有成员类型(C++20 前)
-
仅仅因为
std::unordered_map的实现不会(使用)中断,如果您提供不符合要求的哈希值并不意味着不存在对std::hash的要求。 -
可能值得问一下为什么 c++ 不再 需要 c++20 的这个。我认为这仍然是同一个问题。