On Wednesday, 2 September 2015 at 09:47:16 UTC, BBasile wrote:
On Tuesday, 1 September 2015 at 23:06:50 UTC, John Carter wrote:
C/C++ discussion here....
http://blog.robertelder.org/signed-or-unsigned-part-2/
D rules here...
http://dlang.org/type.html#integer-promotions
It depends on the context.
You should take care of blending signed and unsigned:
comparison error, a is > b but...
---
uint a = 1;
int b = -1;
assert(a < b); // does not throw
---
You should take care to the index type in a loop:
loop that doesn't run at all because of an infered unsigned
index...
---
auto array = new int[](8);
for (auto i = array.length - 1; i > -1; i--)
array[i] = 8;
assert(array[0] == 0); // does not throw
---
I wish the following would be the standard D behaviour (for T ==
U or if floats are involved neither C nor D have a problem)):
int opCmp(T, U)(const(T) a, const(U) b) pure @safe @nogc nothrow
if(isIntegral!T && isIntegral!U && !is(Unqual!T == Unqual!U))
{
alias C = CommonType!(T, U);
static if(isSigned!T && isUnsigned!U && T.sizeof <= U.sizeof)
return (a < 0) ? -1 : opCmp(cast(U)a, b);
else static if(isUnsigned!T && isSigned!U && T.sizeof >=
U.sizeof)
return (b < 0) ? 1 : opCmp(a, cast(T)b);
else return opCmp(cast(C)a, cast(C)b);
}
this is really almost equally fast, but always correct.