How do I obtain the default hash of a user-defined struct

dnspies dspies at ualberta.ca
Thu Apr 3 14:30:00 PDT 2014


On Wednesday, 2 April 2014 at 20:39:47 UTC, FreeSlave wrote:
> On Wednesday, 2 April 2014 at 20:14:31 UTC, dnspies wrote:
>> How can I get the default-hash of a struct I've defined (to be 
>> used as part of the hash for some containing type)?
>
> UserDefined userDefined;
> writeln(typeid(UserDefined).getHash(&userDefined));
>
> Probably there is a better way. I don't like to call typeid for 
> this purpose.

This doesn't work.  It prints two different hashes for equal 
objects.  I meant how do I get the default hash which is used by 
an associative array.

import std.stdio;

struct my_struct {
	int[] arr;
}

void main() {
	my_struct s1;
	s1.arr = [1,2,3];
	my_struct s2;
	s2.arr = [1,2,3];
	writeln(s1 == s2);
	writeln(typeid(my_struct).getHash(&s1));
	writeln(typeid(my_struct).getHash(&s2));
}

true
626617119
2124658624



More information about the Digitalmars-d-learn mailing list