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 the intent explicit:

  • numeric == cast(N)boolean
  • (numeric != 0) == boolean
  1. bool cmp(N left, M right)
  2. int cmp(N left, M right)
    template smartOp(IntFlagPolicy policy)
    pure @safe 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.sticky : 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