How to get hash value of an object?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Nov 28 16:05:31 PST 2016


On 11/27/16 2:10 AM, panmengh wrote:
> How to get hash value of an object?
>
> Use hashOf? or typeid(T).getHash(&o)?

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. It 
doesn't care about opHash or if any of those bytes are actually 
references to the things you want hashed. In fact, for class references, 
it just hashes the bytes that point at the class. Useless.

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

> Does only hashOf with ldc2 return the right value?

If it does, that's a coincidence. Happened to allocate in the same place.

-Steve


More information about the Digitalmars-d-learn mailing list