Implicit conversion rules

Sigg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 21 10:53:45 PDT 2015


I started reading "The D programming Language" earlier, and came 
to the "2.3.3 Typing of Numeric Operators" section which claims 
that "if at least one participant has type ulong, the other is 
implicitly converted to ulong prior to the application and the 
result has type ulong.".

Now I understand reasoning behind it, and know that adding any 
sufficiently negative value to a ulong/uint/ushort will cause an 
underflow as in following example:

     void func() {
         int a = -10;
         ulong b = 0;
         ulong c = a + b;
         writefln("%s", c);
     }

     out: 18446744073709551574

But shouldn't declaring c as auto force compiler to go extra step 
and "properly" deduce result of the "a + b" expression, since its 
already as far as I understand doing magic in the background? 
Basically try to cast rvalues to narrowest type without losing 
precision before evaluating expression.

Or is there a proper way to do math with unsigned and signed 
primitives that I'm not aware of?


More information about the Digitalmars-d-learn mailing list