Positive integers that multiply to a negative value

前端 未结 9 1058
礼貌的吻别
礼貌的吻别 2021-02-07 05:17

I am learning C++ by reading Stroustrup\'s \"Principles and Practice Using C++\".

In the section about pre- and post-conditions there is the following example of functio

9条回答
  •  无人共我
    2021-02-07 05:30

    Since C++11 there is a boolean value you can test:

    std::numeric_limits::is_modulo
    

    If this value is true then signed arithmetic behaves in a wraparound fashion, and there is no undefined behaviour in the original code. A negative value could indeed be produced and so the test in the original code is meaningful.

    For further discussion of is_modulo see here

提交回复
热议问题