implizieter Cast



  • hallo,

    wenn ich einen char mit einem float addiere, wird dann der char impliziet in einen float umgewandelt?

    danke



  • Was hindert dich dran, es auszuprobieren? http://ideone.com/


  • Mod

    Ja. Das ist jedoch kein Cast im eigentlichen Sinne, sondern eine "conversion" ( = Umwandlung). Speziell eine floating-integral conversion, nach 4.9,2 des Standards:

    A prvalue of an integer type or of an unscoped enumeration type can be converted to a prvalue of a floating
    point type. The result is exact if possible. If the value being converted is in the range of values that can
    be represented but the value cannot be represented exactly, it is an implementation-defined choice of either
    the next lower or higher representable value. [ Note: Loss of precision occurs if the integral value cannot
    be represented exactly as a value of the floating type. — end note ] If the value being converted is outside
    the range of values that can be represented, the behavior is undefined. If the source type is bool, the value
    false is converted to zero and the value true is converted to one.

    Diese Umwandlung wird benutzt gemäß der allgemeinen Regeln für Ausdrücke (Kapitel 5):

    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 scoped enumeration type (7.2), no conversions are performed; if the other
    operand does not have the same type, the expression is ill-formed.
    — 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. Then the following
    rules shall be applied to the promoted operands:
    — If both operands have the same type, no further conversion is needed.
    — Otherwise, if both operands have signed integer types or both have unsigned integer types, the
    operand with the type of lesser integer conversion rank shall be converted to the type of the
    operand with greater rank.
    — Otherwise, 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 shall be converted to
    the type of the operand with unsigned integer type.
    — Otherwise, 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 shall
    be converted to the type of the operand with signed integer type.
    — Otherwise, both operands shall be converted to the unsigned integer type corresponding to the
    type of the operand with signed integer type.



  • anscheinend ja 🙂
    danke


Anmelden zum Antworten