通过网络在不同平台上比较 boost::typeindex 类型 hash_code() 是否安全?

Is it safe to compare boost::typeindex type hash_code() across different platforms over network?

可以安全地说,在机器 A 中使用 boost typeindex hash_code 计算一个类型哈希,然后通过网络将这个哈希发送到机器 B,然后在机器 B 中,使用这个哈希来发现机器 A 的类型 "talking about".

归结起来,是boost::typeindex::hash_code跨平台的结果吗? 我可以在 Playstation 4 中计算这个哈希值,然后在 PC 运行 Linux 和另一台 PC 运行 Windows 和 XBox 中检查这个哈希值吗?

我一直在寻找这个答案很长一段时间,但我不能 100% 确定这是肯定的。如果不能保证,我怎么能实现这样的事情?

谢谢!

is the result of boost::typeindex::hash_code cross platform?

绝对不会。

hash_code 甚至不需要为程序的两个连续构建生成相同的值。它不应该在 运行 程序之外的任何地方存储或传送。

how would I be able to implement such thing?

您需要确定性 "type name" 的确定性散列。您需要提供自己的协议来生成 "type name"(例如,一种生成字符串的静态方法),然后您需要使用众所周知的算法(例如 SHA1)对该名称进行哈希处理。

在任何人争论之前...

任何反驳,无论措辞多么仔细,都是错误的。 boost::hash 基于库扩展技术报告 n1836(您可以找到 here

第 6.3.3 (2) 节说:

The return value of operator() is unspecified, except that equal arguments yield the same result. operator() shall not throw exceptions