ushort + ushort = int?

rikki cattermole rikki at cattermole.co.nz
Mon Aug 20 08:42:20 UTC 2018


On 20/08/2018 8:34 PM, Andrey wrote:
> Hello,
> Here is a code that you can execute using online compiler 
> https://run.dlang.io/:
> 
>> import std.stdio;
>> void main()
>> {
>>    ushort first = 5;
>>    ushort second = 1000;
>>
>>    ushort result = first + second;
>>
>>    writeln(result);
>> }
> 
> I hae this error:
>> onlineapp.d(7): Error: cannot implicitly convert expression 
>> cast(int)first + cast(int)second of type int to ushort
> 
> Why they are "int" when I declared them as "ushort"???
> ushort + ushort = ushort, not int.
> 
> In C++ there aren't any such issues...

It's called integer promotion and it originates from C.
And yes C++ does have such support in some variant (I really don't feel 
like comparing the two).


More information about the Digitalmars-d-learn mailing list