This line :
size_t hashVal0 = std::hash<std::string>{}(strInput);
equal these two lines:
std::hash<std::string> szHash;
size_t hashVal = szHash(strInput);
The hashVal0 == hashVal == true.
this is why need {} in first line:
std::hash<KeyType>{}
: This creates an instance of thestd::hash<KeyType>
class. The{}
is used to call the default constructor.(key)
: This calls theoperator()
method on the instance ofstd::hash<KeyType>
, passingkey
as the argument.
more deep dive:
The syntax std::hash<KeyType>(key)
might look like it’s invoking a function, but std::hash<KeyType>
is not a function—it’s a class template. Therefore, std::hash<KeyType>(key)
would be trying to call a constructor of std::hash<KeyType>
that takes an argument of type KeyType
, but no such constructor exists.