How to understand two pairs of parentheses in this

2019-04-17 19:21发布

问题:

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.

回答1:

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



标签: c++ hash