Andrei Alexandrescu wrote:
> std.traits has a Unsigned template. I plan to add two functions:
> signed(x) and unsigned(x), which transform the integral x into the
> signed/unsigned integral of the same size. Generic code could then call
> signed or unsigned wherever necessary. For abs, they'd have to call
> abs(signed(expr)) which I believe clarifies intent and averts bugs like
> mine.
It would also be completely and utterly wrong. The correct replacement
is this:
T result;
if (isSigned!T) {
result = abs(v);
} else {
result = v;
}
...which works, but is a lot less concise than simply saying
'v = abs(v)'. If I ever found myself writing the above in actual code,
I'd be tempted to factor it out into its own function, call that
function 'abs', and consistently use that function instead of the 'abs'
in Phobos.
--
Rainer Deyke - [email protected]