New integer promotion rules

ag0aep6g anonymous at example.com
Wed Jan 17 21:10:12 UTC 2018


On 01/17/2018 09:30 PM, rumbu wrote:
> And here is why is bothering me:
> 
> auto max = isNegative ? cast(Unsigned!T)(-T.min) : cast(Unsigned!T)T.max);
> 
> The generic code above (which worked for all signed integral types T in 
> 2.077) must be rewritten like this in 2.078:
> 
> static if (T.sizeof >= 4)
>     auto max = isNegative ? cast(Unsigned!T)(-T.min) : 
> cast(Unsigned!T)T.max;
> else
>     auto max = isNegative ? cast(Unsigned!T)(-cast(int)T.min) : 
> cast(Unsigned!T)T.max;
> 
> Now I have to translate an 1-liner in a 4-liner all around my project.

So here you prefer the old behavior.

But in your previous post you wrote:

> compiled with 2.077 or 2.078 with -transition=intpromote
> 
> byte b = byte.min;
> ulong u = -b;
> writeln(u);
> 
> outputs correctly 128 

And that's the new behavior.

You can't have both. You either get to keep the shorter version of your 
max code, or you get "correct" results on `ulong u = -b;`.

Except you can actually (almost) keep your max code, as far as I see. 
Under the old rules `-T.min` is just `T.min` again. So you can just drop 
the negation there and get the same result. And then it works under the 
new rules too, because there's no negation involved.

I.e.: auto max = isNegative ? cast(Unsigned!T)(T.min) : 
cast(Unsigned!T)T.max;


More information about the Digitalmars-d-learn mailing list