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

Examples

1 import checkedint.sticky : smartOp; // smartOp.cmp() never throws
2 
3 assert(uint.max == -1);
4 assert( smartOp.cmp!"!="(uint.max, -1));
5 assert(-3156 > 300u);
6 assert( smartOp.cmp!"<"(-3156, 300u));
7 
8 assert(!smartOp.cmp!"<"(1, real.nan));
9 assert(!smartOp.cmp!"<"(real.nan, 1));

Meta