Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are curly braces in hash function?

Tags:

c++

c++11

In C++11, it is possible to get a hashed value for a string variable as follows:

std::size_t h1 = std::hash<std::string>{}("Some_String");

It is clean and simple. However, I have two questions:

  1. Why do we need the curly braces here?
  2. Is it possible to escape using the braces?
like image 430
MTMD Avatar asked Oct 19 '18 02:10

MTMD


1 Answers

The curly braces are used to value-initialize an object of type std::hash<std::string>. That object can then be called, since it has an overloaded operator(). Alternatively, you could create a named object:

std::hash<std::string> H;
auto h1 = H("Some_String");
like image 145
Brian Bi Avatar answered Nov 19 '22 04:11

Brian Bi