Why is this not a warning?

Uranuz via Digitalmars-d digitalmars-d at puremagic.com
Sat Mar 19 05:57:06 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).

Yep. Integer promotions in D sucks! I like this example:

import std.stdio;

void main()
{
	short a = 10;
	short b = 5;
	short c = a - b;
}

It gives error: Error: cannot implicitly convert expression 
(cast(int)a - cast(int)b) of type int to short

Why I can't substract two values of the same type and assign to 
the variable of the same type directly without casts?! What a 
nonsense!?


More information about the Digitalmars-d mailing list