compare

Compares two integral values with additional static checkings.

If the comparison mixes signed and unsigned operands then the function tries to widen the unsigned operand to perform a valid comparison, otherwise a DMD-style warning is emitted.

bool
compare
(
string op
L
R
string fname = __FILE__
int line = __LINE__
)
(
auto ref L lhs
,
auto ref R rhs
)
if (
(
isIntegral!R &&
isIntegral!L
)
&&
op == "<"
||
op == ">"
||
op == "<="
||
op == ">="
||
op == "=="
||
op == "!="
)

Parameters

op

The comparison operator, must be either >, < , <= or >=. Equality is also allowed even if this is always a transparent operation.

lhs L

The left operand, an integer.

rhs R

The right operand, an integer.

Return Value

Type: bool

A bool, the comparison result.

Examples

int a = -1; uint b;
assert(a > b); // wrong result
assert(compare!">"(a,b) == false); // fixed by operand widening
assert(b < a); // wrong result
assert(compare!"<"(b,a) == false); // fixed by operand widening

long aa = -1; ulong bb;
assert(aa > bb); // wrong result
assert(compare!">"(aa,bb) == true); // not statically fixable
assert(bb < aa); // wrong result
assert(compare!"<"(bb,aa) == true); // not statically fixable

assert(compare!"!="(bb,aa) == true); // test for equality is always transparent OP

immutable long aaa = -1; const ulong bbb;
assert(compare!">"(aaa,bbb) == true);

Meta