How to understand two pairs of parentheses in this

2019-04-17 18:49发布

This code is from C++ primer p.446:

return hash<string>() (sd.isbn());

I don't understand the return expression with two pairs of parentheses. There's no similar syntax in front of the book.

标签: c++ hash
1条回答
ゆ 、 Hurt°
2楼-- · 2019-04-17 19:07

std::hash is a class type. What you are doing here is constructing a temporary std::hash with hash<string>() and then (sd.isbn()) calls the operator() of that temporary passing it sd.isbn().

It would be the same as

std::hash<std::string> temp;
return temp(sd.isbn());

For more reading on using objects that have a operator() see: C++ Functors - and their uses

查看更多
登录 后发表回答