New integer promotion rules
rumbu
rumbu at rumbu.ro
Wed Jan 17 22:30:11 UTC 2018
On Wednesday, 17 January 2018 at 21:12:07 UTC, Rubn wrote:
> On Wednesday, 17 January 2018 at 20:30:07 UTC, 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.
>
> Or write some wrapper code, which you prob should have done in
> the first place if you use that all around your project:
>
> auto max = myMaxFunc!(isNegative, T);
"max" was just an over-templated example to highlight the
problem, code like "m = n < 0 ? -n : n" doesn't worth a wrapper,
for example.
But the original questions remain:
1. Why do I need explicitely to promote my byte to int in order
to assign it to an ulong?
2.077: ulong u = -b; 2.088: ulong u = -cast(int)b;
2. Why do I need a cast(int) to assign a byte to an ubyte, since
I'm explicitely cast it?
2.077: ubyte c = cast(ubyte)-b; 2.088: ubyte c =
cast(ubyte)-cast(int)b
More information about the Digitalmars-d-learn
mailing list