Holes in structs and opEquals

Don nospam at nospam.com
Sun Mar 7 11:15:26 PST 2010


bearophile wrote:
> This comes from a small thread that is going on in digitalmars.D.learn.
> 
> This program asserts:
> 
> 
> import std.c.string;
> struct S { // 16 bytes, with a hole
>     ushort s;
>     double d;
> }
> void main() {
>     S s1, s2;
>     memset(&s1, ubyte.min, S.sizeof);
>     memset(&s2, ubyte.max, S.sizeof);
>     s1.s = s2.s = 0;
>     s1.d = s2.d = 0;
>     assert(s1 == s2);
> }
> 
> 
> But a correctly implemented opEquals (and opCmp) among structs has to ignore the contents of the holes, because they can be filled with anything, for example if the structs where not initialized (with =void).
> Correctness of basic struct operators is not an optional feature, like properties or even enums. If the opEquals among structs is wrong then it's better to not have it at all.

Yes. Most of the problems with struct opEquals were fixed in bug 3433. 
You've found an annoying case which wasn't covered.



More information about the Digitalmars-d mailing list