Integer conversions(narrowing, widening), undefined behaviour

前端 未结 2 1835
星月不相逢
星月不相逢 2020-11-28 12:38

It was pretty difficult for me to find information on this subject in manner that I could easily understand, so I\'m asking for a review of what I have found.It\'s all about

2条回答
  •  借酒劲吻你
    2020-11-28 13:39

    An integral conversion never produces undefined behaviour (it can produce implementation-defined behaviour).

    A conversion to a type that can represent the value being converted is always well-defined: the value simply stays unchanged.

    A conversion to an unsigned type is always well-defined: the value is taken modulo UINT_MAX+1 (or whatever maximum value the target type admits).

    A conversion to a signed type that cannot represent the value being converted results in either an implementation-defined value, or an implementation-defined signal.

    Note that the above rules are defined in terms of integer values and not in terms of sequences of bits.

提交回复
热议问题