On Friday, 19 July 2013 at 20:26:36 UTC, Gary Willoughby wrote:
Why does this template not have the desired result? I honestly though it would return true.

It is because size_t is an unsigned type. Comparisons between signed and unsigned numbers can be surprising because if either of the items being compared are unsigned, the whole comparison is unsigned.

Negative signed numbers, when interpreted as unsigned, become very large numbers because the bits are flipped when you go negative. cast(ubyte) -1 == 255, and of course, 255 <= 10 is false.



The way I'd do the inBounds is to just use T size instead of size_t size.

template inBounds(T, T size) { snip same stuff }

then
writefln("%s", inBounds!(int, 10).result); // true as expected

Reply via email to