C++ std::hash return type

1.3k Views Asked by At

So I've been reading a lot of articles, documentation pages, posts, benchmarks, etc., concerning the use of std::hash and its standard implementations.

Synopsis

Looking here it seems that std::hash will always return an std::size_t, which following from here is at least 16 bits or 2 bytes in size, though it is implementation-dependent.

But this concerns me. How can I use std::hash on strings then, if I cannot even have the guarantee that the hash will be at least 32 bits (and I would really like it to return 64 bits).

On my particular x64 machine std::size_t is defined as long unsigned int, but apparently this is not guaranteed when I deploy my program.

Qustion

Is there a way around this, so I can know for sure that I get a 64-bit hash returned from std::hash ?

Conclusion

Judging from the comments, std::hash will not be suitable. Thanks!

1

There are 1 best solutions below

0
On

size_t is defined to be able to hold the size of the largest object. That means if you're working on a system where size_t is 2 bytes, your strings are going to be very short. Usually size_t is the same size as a pointer, which is 4 bytes on a 32 bit system and 8 bytes on a 64 bit system. It's likely to be enough.

Having said that, if you think that's not enough, you can create your own Hash class, and everything in STL allows you to pass that custom class instead of specializing std::hash.