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
Type: L

The left operand, an integer.

rhs
Type: R

The right operand, an integer.

Return Value

Type: bool

A bool, the comparison result.

Examples

1 int a = -1; uint b;
2 assert(a > b); // wrong result
3 assert(compare!">"(a,b) == false); // fixed by operand widening
4 assert(b < a); // wrong result
5 assert(compare!"<"(b,a) == false); // fixed by operand widening
6 
7 long aa = -1; ulong bb;
8 assert(aa > bb); // wrong result
9 assert(compare!">"(aa,bb) == true); // not statically fixable
10 assert(bb < aa); // wrong result
11 assert(compare!"<"(bb,aa) == true); // not statically fixable
12 
13 assert(compare!"!="(bb,aa) == true); // test for equality is always transparent OP
14 
15 immutable long aaa = -1; const ulong bbb;
16 assert(compare!">"(aaa,bbb) == true);

Meta