short s, t; t = -s: no (longer) works: Deprecation: integral promotion not done for -s, use

Steven Schveighoffer schveiguy at yahoo.com
Sat Feb 24 22:30:09 UTC 2018


On 2/24/18 4:42 PM, kdevel wrote:
> On Saturday, 24 February 2018 at 20:17:12 UTC, Steven Schveighoffer wrote:
>> https://dlang.org/changelog/2.078.0.html#fix16997
> 
> My goodness! So there is currently no negation operator defined on short 
> and some other types?

No, that's not the case. It's simply defined incorrectly.

The prime example is this:

byte b = -128;

int x = -b;

What would you expect x to be?

a) 128
b) -128

Currently, the answer is b. In C, the answer is a. With the 
-transition=intpromote switch, the answer is changed to a.

The reason it's so annoying is because we can't break code without first 
warning about it. This will change behavior in some cases. But chances 
are in most cases, you really wanted what C did, or your code would 
never hit the corner cases anyway (byte.min and short.min are so rare in 
the wild).

Eventually, the intpromote switch will go away, and a will be the 
permanent answer.

> Any objections against leaving out the compiler switch and using
> 
>     b[i] = cast (T) (0 - b[i]);
> 
> instead?

You can do that too, seems like a good workaround. The current 
requirement that you first have to cast to int, and then cast back, is a 
bit over the top. See here: https://issues.dlang.org/show_bug.cgi?id=18380

-Steve


More information about the Digitalmars-d-learn mailing list