On Thursday, 17 March 2016 at 12:35:27 UTC, Basile B wrote:
import std.traits;
bool safeIntegralCmp(string op, L, R)(auto ref L lhs, auto ref
R rhs)
if (isIntegral!R && isIntegral!L)
{
// safe
static if (is(Unqual!L == Unqual!R))
{
mixin("return lhs" ~ op ~ "rhs;");
}
else
{
// promote unsigned to bigger signed
static if (isSigned!L && R.sizeof < 8)
{
long widenedRhs = rhs;
mixin("return lhs" ~ op ~ "widenedRhs;");
}
else static if (isSigned!R && L.sizeof < 8)
{
long widenedLhs = lhs;
mixin("return widened" ~ op ~ "rhs;");
}
// not fixable by operand widening
else
{
pragma(msg, "warning, comparing a" ~ L.stringof ~ "
with a" ~ R.stringof
~ " may result into wrong results");
mixin("return lhs" ~ op ~ "rhs;");
}
}
}
Obviously I meant to write this:
[...]
static if (isSigned!L && !isSigned!R && R.sizeof < 8)
[...]
else static if (isSigned!R && !isSigned!L && L.sizeof < 8)
It makes more sense...