Does std::hash give same result for same input for different compiled builds and different machines?

杀马特。学长 韩版系。学妹 提交于 2020-07-20 08:02:35

问题


I have some random test parameters for which I need to calculate a hash to detect if I ran with same parameters. I might run the test using the same source recompiled at a different time or run on a different machine.

Even so I want to detect whether the same parameters were used for the run. Does std::hash give the same result for the same input for different compiled builds and different machines?

e.g.

std::hash<string>{}("TestcaseParamVal0.7Param0.4");

Will this always be a unique number?


回答1:


No, std::hash does not guarantee that the result will be the same across computers, builds, or even executions of the same build on the same computer. Your only guarantee is that during one execution, objects that are equal have the same hash. (There is, of course, no guarantee that objects that are unequal have different hashes.)

Some implementations go out of their way to change hash results between executions, as it mitigates denial of service risks due to the poor performance of hash tables in the presence of many keys with the same hash. This is explicitly allowed by the standard, which only guarantees that results are consistent for the duration of the program.

If you need repeatability between executions and machines, you can't use std::hash and must roll out your own equivalent.



来源:https://stackoverflow.com/questions/51145320/does-stdhash-give-same-result-for-same-input-for-different-compiled-builds-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!