Why is this not a warning?

Mathias Lang via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 16 11:39:36 PDT 2016


On Wednesday, 16 March 2016 at 16:40:49 UTC, Shachar Shemesh 
wrote:
> Please consider the following program, which is a reduced 
> version of a problem I've spent the entire of today trying to 
> debug:
>
> import std.stdio;
>
> void main() {
>     enum ulong MAX_VAL = 256;
>     long value = -500;
>
>     if( value>MAX_VAL )
>         value = MAX_VAL;
>
>     writeln(value);
> }
>
> People who are marginally familiar with integer promotion will 
> not be surprised to know that the program prints "256". What is 
> surprising to me is that this produced neither error nor 
> warning.
>
> The comparable program in C++, when compiled with gcc, 
> correctly warns about signed/unsigned comparison (though, to be 
> fair, it seems that clang doesn't).

I find integer promotion/comparison rules to be one of the 
messier part of D at the moment. E.g. the compiler won't say 
anything about `ulong foo = -1;` either.
Sadly, to solve that without imposing much pain on the users, you 
need a more decent VRP than we currently have, for example the 
following should compile:

```
void main ()
{
     ushort f;
     uint i;

     if (i < ushort.max)
         f = i;
}
```


More information about the Digitalmars-d mailing list