Bug? 0 is less than -10

Dominikus Dittes Scherkl via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 8 02:01:30 PDT 2015


On Wednesday, 7 October 2015 at 16:25:02 UTC, Marc Schütz wrote:
> Lionello Lunesu posted a PR that should fix this:
> https://github.com/D-Programming-Language/dmd/pull/1913
> See also the discussion in the linked bug report.
>
> Unfortunately it seems it's been forgotten since then...

Meanwhile I have even improved my solution posted to fix bug #259 
to use less casts:

int opCmp(T, U)(const(T) a, const(U) b) pure @safe @nogc nothrow
    if(isIntegral!T && isIntegral!U && !is(Unqual!T == Unqual!U))
{
    static if(isSigned!T && isUnsigned!U && T.sizeof <= U.sizeof)
    {
       return (a < 0) ? -1 : opCmp(cast(U)a, b);
    }
    else static if(isUnsigned!T && isSigned!U && T.sizeof >= 
U.sizeof)
    {
       return (b < 0) ? 1 : opCmp(a, cast(T)b);
    }
    else // both signed or both unsigned or the unsigned type is 
smaller
    {
       // do what the compiler always did so far:
       alias C = CommonType!(T, U); // use the larger of the both
       return opCmp(cast(C)a, cast(C)b);
    }
}

And on comparison with number literals everything will be 
optimized away, so no execution timer overhead at all!


More information about the Digitalmars-d-learn mailing list