Why does this template not have the desired result?
Adam D. Ruppe
destructionator at gmail.com
Sat Jul 20 14:27:19 PDT 2013
On Friday, 19 July 2013 at 22:18:54 UTC, Gary Willoughby wrote:
> The problem with that though is that with size arguments >
> int.max will wrap when being cast to int (T)? i.e.:
I see. The only way to guarantee there's no wraparound with a
literal is to use a string.... which messes up the arithmetic but
works for anything:
template inBounds(T, string size)
{
import std.conv;
enum result = (T.min <= to!T(size));// && size <= T.max);
}
writefln("%s", inBounds!(int, "-5995534353510").result); //
false! eh?
That would throw at compile time - to checks for overflows, and
since it is an enum in a template, it is run in CTFE.
So kinda awkward but would work fairly reliabily.
More information about the Digitalmars-d-learn
mailing list