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

290 views Asked by At

Is it safe to say, compute a type hash using boost typeindex hash_code in machine A, then send this hash to machine B over the network, and then, in the machine B, use this hash to discover the type the machine A was "talking about".

Boils down to, is the result of boost::typeindex::hash_code cross platform? Can I compute this hash in, say a Playstation 4, and then check this hash in a PC running Linux and in another PC running Windows and in an XBox?

I've been looking for this answer for a long time now, and I'm not 100% sure this is guaranteed. If this is not guaranteed, how would I be able to implement such thing?

Thank you!

1

There are 1 answers

1
Richard Hodges On BEST ANSWER

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

Absolutely not.

hash_code is not even required to produce the same value for two consecutive builds of the program. It should never be stored or communicated anywhere outside a running program.

how would I be able to implement such thing?

You'd want a deterministic hash of a deterministic "type name". You would need to provide your own protocol for producing the "type name" (a static method which produces a string, for example) and then you would need to hash that that name with a well known algorithm (SHA1, for example).

Before anyone argues...

Any counter-arguments, no matter how carefully phrased are wrong. boost::hash is based on the library extensions technical report n1836 (which you can find here)

Section 6.3.3 (2) says:

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