[Issue 21109] Wrong result when using sort() on enum arrays

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Aug 4 09:42:04 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=21109

Boris Carvajal <boris2.9 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |boris2.9 at gmail.com

--- Comment #5 from Boris Carvajal <boris2.9 at gmail.com> ---
There is no 'opCmp' defined for the struct so the runtime just use a memcmp for
the comparison.
The code itself calls '__cmp' from core/internal/array/comparison.d (because
the element are an array '[S(2, [0, 0])]') and there the 3 'static if'
conditions fail but if you define 'opCmp' the second path is taken.

So the following works:

struct S
{
    int[] arr;
    int opCmp(ref const S s) const { return arr < s.arr; }
}

There is a comment on clone.d file that says:

"Essentially, a struct which does not define opCmp is not comparable."

This concise phrase should be on the spec.

--


More information about the Digitalmars-d-bugs mailing list