在Stroustrup的新书"The C++ Programming Language - Fourth Edition"的第10.5.1节中,他说,在执行算术运算之前,使用整数提升来创建较短整数类型的整数,同样,浮点数提升也是如此.用于从浮动中创建双打.
我用以下代码确认了第一个索赔:
#include <iostream>
#include <typeinfo>
int main()
{
short a;
short b;
std::cout << typeid(a + b).name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这用vc ++输出"int",用gcc输出"i".
但是用浮点数而不是短路来测试它,输出仍然是"浮动"或"f":
#include <iostream>
#include <typeinfo>
int main()
{
float a;
float b;
std::cout << typeid(a + b).name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
根据Stroustrup,浮点提升规则没有例外,所以我期望输出"double"或"d".
关于促销的提到的部分是错误的还是某种程度上不清楚?关于类型促销,C++ 98和C++ 11有什么区别吗?