Why is {} used to access operator() in std::hash?

佐手、 提交于 2019-12-05 09:09:35

std::hash<T> is a type not a function.

An instance of std::hash has an operator() that does the hash.

So std::hash<std::string> is a hashing type. {} then creates an instance of that type. (s.first_name) calls operator() on a std::hash<std::string>.

std::hash<std::string>{}(s.first_name);
^                     ^       ^
|                     |   call operator() on that instance
type of hasher        |
                create an instance of that type

std::hash is not a function, but a class, more specifically a functor. So you have to create an object of that class before you can call its operator().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!