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.

Reply via email to