Ordering comparisons
Andrei Alexandrescu via Digitalmars-d
digitalmars-d at puremagic.com
Mon Mar 6 17:27:56 PST 2017
While reviewing work on array comparisons, Vladimir found an odd issue:
https://issues.dlang.org/show_bug.cgi?id=17244
Investigation reveals that during array comparison for inequality,
structs are compared by means of memcmp - even if they don't define
opCmp, and regardless of the types of their fields. This has obvious
unpleasant consequences:
* Floating point values don't compare well with memcmp
* A struct S { int x; } compares differently on little endian and big
endian system (!)
* A struct containing a field that defines opCmp ignores it
* ... and many other nefarious issues
The question is what to do to minimize breakage yet "break the bad
code". The most backward-compatible solution is to define opCmp
automatically to do a field-by-field lexicographical comparison. The
most radical solution is disable ordering comparisons unless explicitly
implemented by the user.
Andrei
More information about the Digitalmars-d
mailing list