Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ hash<string> is there a way to get the same value in linux and windows

Tags:

c++

hash

I try to find a way to get the same result when I hash a given string in windows and in linux. but for example if I run the following code:

hash<string> h;
cout << h("hello");

it will return 3305111549 in windows and 2762169579135187400 in linux.

If it is not possible to get the same return value accross these 2 platforms, is there any other decent hash function that would return the same value on both systems?

like image 773
abass.mahdavi Avatar asked Jan 30 '23 06:01

abass.mahdavi


1 Answers

No. As per std::hash reference, emphasis mine:

The actual hash functions are implementation-dependent and are not required to fulfill any other quality criteria except those specified above.

More specifically you are using the std::hash<std::string> template specialization whose hashes:

equal the hashes of corresponding std::basic_string_view classes

which are also implementation dependent. So no, you can not expect the same std::hash results with different implementations. Furthermore since C++14:

Hash functions are only required to produce the same result for the same input within a single execution of a program;

like image 150
Ron Avatar answered Mar 15 '23 23:03

Ron