Integer promotion issue

Guillaume Piolat first.last at gmail.com
Sun Jan 26 13:01:58 UTC 2020


On Sunday, 26 January 2020 at 00:22:14 UTC, Adam D. Ruppe wrote:
> D inherited a silly rule from C (all arithmetic yields int or 
> above)

It seems silly however in the wild this C int promotion rule make 
stuff work without the author knowledge.


Example:

--------------------------------------------------------


void computeMean(short* )
{
     // let's compute the mean of two arrays
     foreach(n; 0..N)
     {
         short a = input[n];
         short b = input2[n];
         int meanOfTwo = (a + b) >> 1; // BOOM
     }
}

--------------------------------------------------------

Pretty sure a LOT of code would break if C++ or D would abandon 
this rule (well, except you introduce traps for integer overflow).

Doing it without the rule introduce a lot of casts...


More information about the Digitalmars-d mailing list