【发布时间】:2018-02-22 09:10:50
【问题描述】:
在阅读用于 std::unordered_map 的 std::hash 示例时,我注意到 {} 正在访问 operator() 函数。
http://en.cppreference.com/w/cpp/utility/hash
result_type operator()(argument_type const& s) const
{
result_type const h1 ( std::hash<std::string>{}(s.first_name) );
result_type const h2 ( std::hash<std::string>{}(s.last_name) );
return h1 ^ (h2 << 1); // or use boost::hash_combine (see Discussion)
}
这里使用的 {} 代表什么?
【问题讨论】:
-
我希望 C++ 允许
static operator()s. -
@DanielH 为什么? C++ 允许静态函数,函数的名称并不重要,不是吗?您希望将状态附加到您调用的函数,这正是仿函数的优势(或用例)
-
@DanielH 这基本上是......返回您选择的类型的构造函数。疯狂。
标签: c++ c++11 hash unordered-map c++-standard-library