smartOp.cmp

Compare left and right using op.

  • Unlike the standard integer comparison operator, this function correctly handles negative values in signed/unsigned comparisons.
  • Like the standard operator, comparisons involving any floating-point nan value always return false.


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)
    template smartOp(IntFlagPolicy policy)
    pure @safe nothrow @nogc
    static if(policy == IntFlagPolicy.none)
    bool
    cmp
    (
    string op
    N
    M
    )
    (
    const N left
    ,
    const M right
    )
    if (
    isScalarType!N &&
    isScalarType!M
    )
  2. int cmp(N left, M right)
  3. alias cmp = smartOp!(IntFlagPolicy.none).cmp

Examples

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

assert(uint.max == -1);
assert( smartOp.cmp!"!="(uint.max, -1));
assert(-3156 > 300u);
assert( smartOp.cmp!"<"(-3156, 300u));

assert(!smartOp.cmp!"<"(1, real.nan));
assert(!smartOp.cmp!"<"(real.nan, 1));

Meta