Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

hash

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.

like image 752
tocrafty Avatar asked Oct 13 '16 14:10

tocrafty


People also ask

What does 2 parentheses mean in Python?

print(funcwrapper(3, 2)) So what does the first example using two pairs of parentheses accomplish? The use of a double parentheses is actually an indicator of one of Python's coolest features - and that is that functions are themselves, objects!

How do you know if a stack is balanced parentheses?

One approach to check balanced parentheses is to use stack. Each time, when an open parentheses is encountered push it in the stack, and when closed parenthesis is encountered, match it with the top of stack and pop it. If stack is empty at the end, return Balanced otherwise, Unbalanced.

Are parentheses balanced?

The balanced parenthesis means that when the opening parenthesis is equal to the closing parenthesis, then it is a balanced parenthesis.


1 Answers

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

like image 71
NathanOliver Avatar answered Sep 28 '22 14:09

NathanOliver