我想更好地知道我应该什么时候施放。C++中加法、乘法等的隐式类型转换规则是什么,例如,
int + float = ? int * float = ? float * int = ? int / float = ? float / int = ? int / int = ? int ^ float = ?
等等…
表达式是否总是被评估为更精确的类型?Java的规则是否不同?如果我对这个问题的措辞不准确,请纠正我。
在 C++ 中,运算符(对于 POD 类型)总是作用于相同类型的对象。 因此,如果它们不相同,则将提升以匹配另一个。 运算结果的类型与操作数相同(转换后)。
if: either is long double other is promoted > long double either is double other is promoted > double either is float other is promoted > float either is long long unsigned int other is promoted > long long unsigned int either is long long int other is promoted > long long int either is long unsigned int other is promoted > long unsigned int either is long int other is promoted > long int either is unsigned int other is promoted > unsigned int either is int other is promoted > int Otherwise: both operands are promoted to int
笔记。操作的最小规模是int。所以short/char被提升到int在操作完成之前。
int
short
char
在您的所有表达式中,在执行操作之前将int提升为 a 。float操作的结果是一个float。
float
int + float => float + float = float int * float => float * float = float float * int => float * float = float int / float => float / float = float float / int => float / float = float int / int = int int ^ float => <compiler error>