c++ only: unary minus for 0x80000000

后端 未结 2 1467
长情又很酷
长情又很酷 2021-01-21 05:31

This question is supposedly for language-lawyers.

Suppose that signed and unsigned int are both 32 bits wide. As stated in the n3337.pdf draft, 5.3.1.8,

(-

2条回答
  •  灰色年华
    2021-01-21 05:59

    Signed integer overflow is always undefined, as far as I know. From the C++ spec section 5 Expressions, paragraph 4:

    If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. [Note: most existing implementations of C++ ignore integer overflows. Treatment of division by zero, forming a remainder using a zero divisor, and all floating point exceptions vary among machines, and is usually adjustable by a library function. —endnote]

提交回复
热议问题