smartOp.cmp

Defines a total order on all basic scalar values, using the same rules as std.math.cmp().

  • Mixed signed/unsigned comparisons return the mathematically correct result.
  • If neither left nor right is floating-point, this function is faster than std.math.cmp().
  • If either left or right is floating-point, this function forwards to std.math.cmp().


Direct comparisons between boolean values and numeric ones are forbidden. Make your intention explicit:

  • cast(N)boolean == numeric
  • boolean == (numeric != 0)
  1. bool cmp(N left, M right)
  2. int cmp(N left, M right)
    template smartOp(IntFlagPolicy policy)
    @safe pure nothrow @nogc
    static if(policy == IntFlagPolicy.none)
    int
    cmp
    (
    N
    M
    )
    (
    const N left
    ,
    const M right
    )
    if (
    isScalarType!N &&
    isScalarType!M
    )
  3. alias cmp = smartOp!(IntFlagPolicy.none).cmp

Examples

import checkedint.noex : smartOp; // smartOp.cmp() never throws

assert(smartOp.cmp(325.0, 325u) == 0);
assert(smartOp.cmp(uint.max, -1) == 1);
assert(smartOp.cmp(-3156, 300u) == -1);

Meta