How to implement opCmp?

Honey via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 11 08:40:42 PDT 2017


On Sunday, 11 June 2017 at 15:24:30 UTC, Honey wrote:
> Doesn't it make sense to introduce another overload of cmp 
> similar to Steve's doCmp [2] right at that spot?

Moreover, it seems that std.algorithm.cmp should employ three way 
comparison as well. The current implementation

int cmp(alias pred = "a < b", R1, R2)(R1 r1, R2 r2)
if (isInputRange!R1 && isInputRange!R2 && !(isSomeString!R1 && 
isSomeString!R2))
{
     for (;; r1.popFront(), r2.popFront())
     {
         if (r1.empty) return -cast(int)!r2.empty;
         if (r2.empty) return !r1.empty;
         auto a = r1.front, b = r2.front;
         if (binaryFun!pred(a, b)) return -1;
         if (binaryFun!pred(b, a)) return 1;
     }
}

does not seem to be great.


More information about the Digitalmars-d-learn mailing list