Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hash_value function in C++11

Tags:

c++

c++11

boost

The Boost library provides a convenience function hash_value which basically just called:

return hash<T>()(key);

As far as I can see, C++11 included std::hash which is pretty similar to boost::hash, but did not include std::hash_value. This requires application code to create a hash object and call it's operator() instead of just calling a convenient function. Is there some reason that std::hash_value was not standardized?

like image 334
Siler Avatar asked Nov 19 '13 23:11

Siler


1 Answers

The primary use of the std::hash<T> function is the object used to obtain a hash value from a key in the std::unordered_* group of containers. These will always contain and use a corresponding object, possibly, using the empty base optimization to avoid it taking any memory. In any case, whenever the std::hash<T> type is used, an object is actually around.

Although the function object can be used stand-alone, it is probably rare. Also, for other, similar existing function objects there are no corresponding convenience calling functions: although most of them are wrappers for operators, especially std::less<void*> could be interesting to call stand-alone as you can't use ptr1 < ptr2 (at least, it couldn't be used in C++03 if ptr1 and ptr2 were not part of the same array object). That is, there was no suitable precedence.

Finally, I would guess that the convenience function was simply not part of the proposal: if it isn't proposed and there isn't a really good case to have, nothing will be included into the C++ standard. From the looks of it n1456 seems to be, at least, one revision of the "hash table" proposal and it doesn't include a trace of std::hash_value<T>().

like image 171
Dietmar Kühl Avatar answered Oct 20 '22 13:10

Dietmar Kühl