相关疑难解决方法(0)

为什么在C和C++中算术运算之前必须将short转换为int?

从我从得到的回答这个问题,看来C++继承了这一要求,对于转换shortint从C.执行算术运算时,我可以挑你的大脑,以为什么这是用C首先介绍?为什么不做这些操作short呢?

例如(取自评论中的dyp建议):

short s = 1, t = 2 ;
auto  x = s + t ;
Run Code Online (Sandbox Code Playgroud)

x将具有int类型.

c c++ int short integer-promotion

70
推荐指数
4
解决办法
8650
查看次数

添加产生的C++类型是什么

我收到了"模糊调用"编译错误:

short i;
MyFunc(i+1, i+1);
Run Code Online (Sandbox Code Playgroud)

MyFunc有两个定义 - 一个带两个短裤,另一个带两个浮子.

我写的时候:

MyFunc(i, i+1);
Run Code Online (Sandbox Code Playgroud)

没有错误 - 编译器推断短.

我的问题是,为什么"短"+ 1可能会导致浮点,我怎样才能避免遍历所有代码并添加显式的强制转换,例如:

MyFunc((short)(i+1), (short)(i+1));
Run Code Online (Sandbox Code Playgroud)

谢谢.

c++ compiler-errors type-conversion

4
推荐指数
1
解决办法
168
查看次数