Why does this template not have the desired result?

Gary Willoughby dev at nomad.so
Fri Jul 19 15:18:51 PDT 2013


> 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

The problem with that though is that with size arguments > 
int.max will wrap when being cast to int (T)? i.e.:

import std.stdio;

template inBounds(T, T size)
{
	enum result = (size >= T.min && size <= T.max);
}

void main(string[] args)
{
	writefln("%s", inBounds!(int, int.max + 1).result); //true, 
wrong!
}


More information about the Digitalmars-d-learn mailing list