我想知道什么时候该投。在c++中,当进行加法、乘法等操作时,隐式类型转换规则是什么?例如,

int + float = ?
int * float = ?
float * int = ?
int / float = ?
float / int = ?
int / int = ?
int ^ float = ?

等等...

表达式是否总是计算为更精确的类型?Java的规则不同吗? 如果我在这个问题上用词不准确,请指正。


当前回答

If you exclude the unsigned types, there is an ordered hierarchy: signed char, short, int, long, long long, float, double, long double. First, anything coming before int in the above will be converted to int. Then, in a binary operation, the lower ranked type will be converted to the higher, and the results will be the type of the higher. (You'll note that, from the hierarchy, anytime a floating point and an integral type are involved, the integral type will be converted to the floating point type.)

Unsigned让事情变得有点复杂:它会影响排名,而且 部分排名变成实现定义。因为 最好不要把有符号和无符号混合在一起 表达式。(大多数c++专家似乎都避免使用unsigned除非 涉及到按位操作。那至少是什么 Stroustrup建议)。

其他回答

If you exclude the unsigned types, there is an ordered hierarchy: signed char, short, int, long, long long, float, double, long double. First, anything coming before int in the above will be converted to int. Then, in a binary operation, the lower ranked type will be converted to the higher, and the results will be the type of the higher. (You'll note that, from the hierarchy, anytime a floating point and an integral type are involved, the integral type will be converted to the floating point type.)

Unsigned让事情变得有点复杂:它会影响排名,而且 部分排名变成实现定义。因为 最好不要把有符号和无符号混合在一起 表达式。(大多数c++专家似乎都避免使用unsigned除非 涉及到按位操作。那至少是什么 Stroustrup建议)。

在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在执行操作之前被提升为浮点数。操作的结果是一个浮点数。

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>

整个第4章都在讲转换,但我认为你应该对这些最感兴趣:

4.5 Integral promotions [conv.prom] An rvalue of type char, signed char, unsigned char, short int, or unsigned short int can be converted to an rvalue of type int if int can represent all the values of the source type; other- wise, the source rvalue can be converted to an rvalue of type unsigned int. An rvalue of type wchar_t (3.9.1) or an enumeration type (7.2) can be converted to an rvalue of the first of the following types that can represent all the values of its underlying type: int, unsigned int, long, or unsigned long. An rvalue for an integral bit-field (9.6) can be converted to an rvalue of type int if int can represent all the values of the bit-field; otherwise, it can be converted to unsigned int if unsigned int can rep- resent all the values of the bit-field. If the bit-field is larger yet, no integral promotion applies to it. If the bit-field has an enumerated type, it is treated as any other value of that type for promotion purposes. An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true becoming one. These conversions are called integral promotions.

4.6浮点提升 (conv.fpprom) float类型的右值可以转换为double类型的右值。该值不变。 这种转换称为浮点提升。

因此,所有涉及浮点数的转换结果都是浮点数。

只有一个包含两个int -结果是int: Int / Int = Int

表达式的类型,当不是两个部分是相同类型时,将转换为两者中最大的。这里的问题是理解哪一个比另一个大(它与字节大小无关)。

在包含实数和整数的表达式中,整数将升格为实数。例如,在int + float中,表达式的类型是float。

另一个区别与类型的能力有关。例如,包含int型和long int型的表达式的结果为long int型。

涉及浮点数的算术运算结果为浮点数。

int + float = float
int * float = float
float * int = float
int / float = float
float / int = float
int / int = int

更多细节请回答。看看c++标准的§5/9节是怎么说的

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: — If either operand is of type long double, the other shall be converted to long double. — Otherwise, if either operand is double, the other shall be converted to double. — Otherwise, if either operand is float, the other shall be converted to float. — Otherwise, the integral promotions (4.5) shall be performed on both operands.54) — Then, if either operand is unsigned long the other shall be converted to unsigned long. — Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int. — Otherwise, if either operand is long, the other shall be converted to long. — Otherwise, if either operand is unsigned, the other shall be converted to unsigned. [Note: otherwise, the only remaining case is that both operands are int ]