tocrafty tocrafty - 1 month ago 6
C++ Question

How to understand two pairs of parentheses in this code fragment?

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.

Answer

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