ushort + ushort = int?

Jim Balter Jim at Balter.name
Tue Aug 21 19:06:46 UTC 2018


On Monday, 20 August 2018 at 08:34:56 UTC, 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...

Only if you make the mistake of compiling without warnings. 
Otherwise, the C++ compiler will warn that you're converting an 
int to a ushort.


More information about the Digitalmars-d-learn mailing list