Implicit type conversion rules in C++ operators

后端 未结 9 2107
情书的邮戳
情书的邮戳 2020-11-22 00:21

I want to be better about knowing when I should cast. What are the implicit type conversion rules in C++ when adding, multiplying, etc. For example,

int + fl         


        
9条回答
  •  一整个雨季
    2020-11-22 00:29

    My solution to the problem got WA(wrong answer), then i changed one of int to long long int and it gave AC(accept). Previously, I was trying to do long long int += int * int, and after I rectify it to long long int += long long int * int. Googling I came up with,

    1. Arithmetic Conversions

    Conditions for Type Conversion:

    Conditions Met ---> Conversion

    • Either operand is of type long double. ---> Other operand is converted to type long double.

    • Preceding condition not met and either operand is of type double. ---> Other operand is converted to type double.

    • Preceding conditions not met and either operand is of type float. ---> Other operand is converted to type float.

    • Preceding conditions not met (none of the operands are of floating types). ---> Integral promotions are performed on the operands as follows:

      • If either operand is of type unsigned long, the other operand is converted to type unsigned long.
      • If preceding condition not met, and if either operand is of type long and the other of type unsigned int, both operands are converted to type unsigned long.
      • If the preceding two conditions are not met, and if either operand is of type long, t he other operand is converted to type long.
      • If the preceding three conditions are not met, and if either operand is of type unsigned int, the other operand is converted to type unsigned int.
      • If none of the preceding conditions are met, both operands are converted to type int.

    2 . Integer conversion rules

    • Integer Promotions:

    Integer types smaller than int are promoted when an operation is performed on them. If all values of the original type can be represented as an int, the value of the smaller type is converted to an int; otherwise, it is converted to an unsigned int. Integer promotions are applied as part of the usual arithmetic conversions to certain argument expressions; operands of the unary +, -, and ~ operators; and operands of the shift operators.

    • Integer Conversion Rank:

      • No two signed integer types shall have the same rank, even if they have the same representation.
      • The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision.
      • The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.
      • The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.
      • The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width.
      • The rank of char shall equal the rank of signed char and unsigned char.
      • The rank of any extended signed integer type relative to another extended signed integer type with the same precision is implementation-defined but still subject to the other rules for determining the integer conversion rank.
      • For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 has greater rank than T3.
    • Usual Arithmetic Conversions:

      • If both operands have the same type, no further conversion is needed.
      • If both operands are of the same integer type (signed or unsigned), the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
      • If the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type is converted to the type of the operand with unsigned integer type.
      • If the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, the operand with unsigned integer type is converted to the type of the operand with signed integer type.
      • Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type. Specific operations can add to or modify the semantics of the usual arithmetic operations.

提交回复
热议问题