How to get hash value of an object?

Era Scarecrow via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 4 05:17:09 PST 2016


On Tuesday, 29 November 2016 at 00:05:31 UTC, Steven 
Schveighoffer wrote:
> hashOf is kind of this horrible hacky thing that nobody should 
> be using. It literally takes whatever you pass it and hashes 
> the local bytes.

  Ugg... Anything with pointers, classes or arrays will have huge 
problems with consistency; While anything with fixed static 
arrays or pure value-types will result in proper values.

  I'd almost prefer an option to get hashOf all inner object types 
and then xor them all together. Although this could make for a 
very complex hashOf depending on implementation of the object.

> Long story short, use typeid(T).getHash(&o).

  Hmmm maybe make any struct have hashOf disabled if it includes 
any pointers or dynamic arrays or inner objects that can't get a 
proper hash of it. While it will break things, at least you know 
what can't be hashed when you try to use it. Or maybe have that 
as a compiler option for those wanting to make use of that.


More information about the Digitalmars-d-learn mailing list