c++ only: unary minus for 0x80000000

后端 未结 2 1447
长情又很酷
长情又很酷 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 06:12

    Signed integral types obey the rules of mathematical integers without added computer bullshit. So -std::numeric_limits< signed_type >::min() is going to be undefined behavior, if the given type cannot represent the resulting number.

    In a constexpr, the implementation is required to reject that expression, as anything causing undefined behavior renders a constant expression invalid, as a diagnosable rule. In this case the rule is one of the forbidden items in §5.19,

    — a result that is not mathematically defined or not in the range of representable values for its type;

    In constant folding, the compiler is most likely to insert the overflowed value.

提交回复
热议问题