Why does this template not have the desired result?
JS
js.mdnq at gmail.com
Sat Jul 20 03:14:56 PDT 2013
On Friday, 19 July 2013 at 22:18:54 UTC, Gary Willoughby wrote:
>> 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!
> }
A warning should be thrown for this type of behavior... it can
result in pretty serious and hard to find bugs.
More information about the Digitalmars-d-learn
mailing list