"float = float - float" 中是否有隐式类型提升?
Is there an implicit type promotion in "float = float - float"?
我们正在使用 QA-C 来实现 MISRA C++ 一致性,但该工具针对如下代码发出错误:
float a = foo();
float b = bar();
float c = a - b;
据我所知,这没有隐式类型提升,因为一切都将发生在 float
大小的块中,但该工具告诉我减法会导致一个。有没有隐性提升的情况?
这里不涉及隐式提升。
涉及二元运算符的转换时,称为普通算术转换。
根据 C++ 标准,[expr]/11
:
11 Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield
result types in a similar way. The purpose is to yield a common type, which is also the type of the result.
This pattern is called the usual arithmetic conversions, which are defined as follows:
...
(11.4) — Otherwise, if either operand is float
, the other shall be converted to float
.
由于在您的示例中两个操作数都是 float
,因此没有此类转换或提升。
所以这可能是该工具的误报。
我们正在使用 QA-C 来实现 MISRA C++ 一致性,但该工具针对如下代码发出错误:
float a = foo();
float b = bar();
float c = a - b;
据我所知,这没有隐式类型提升,因为一切都将发生在 float
大小的块中,但该工具告诉我减法会导致一个。有没有隐性提升的情况?
这里不涉及隐式提升。
涉及二元运算符的转换时,称为普通算术转换。
根据 C++ 标准,[expr]/11
:
11 Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:
...
(11.4) — Otherwise, if either operand isfloat
, the other shall be converted tofloat
.
由于在您的示例中两个操作数都是 float
,因此没有此类转换或提升。
所以这可能是该工具的误报。