Opinions: The Best and Worst of D (for a lecture/talk I intend to give)

Dominikus Dittes Scherkl via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 10 05:53:03 PDT 2014


So at all the implementation will look something like this:

int opCmp(T, U)(const(T) a, const(U) b) @primitive 
if(isIntegral!T && isIntegral!U)
{
    alias CommonType!(Signed!T, Signed!U) C;

    static if(isSigned!T && isUnsigned!U)
    {
       return (b > cast(Unsigned!C)C.max) ? -1 : cast(C)a - 
cast(C)b;
    }
    else static if(isUnsigned!T && isSigned!U)
    {
       return (a > cast(Unsigned!C)C.max) ? 1 : cast(C)a - 
cast(C)b;
    }
    else // both signed or both unsigned
    {
       return cast(C)a - cast(C)b;
    }
}

And it will be just as fast as ever, except if you compare apples 
with peaches where it take a tick longer but give the correct 
result anyway


More information about the Digitalmars-d-learn mailing list