No useful and reliable way to detect integer overflow in C/C++?

后端 未结 4 1334
温柔的废话
温柔的废话 2021-02-08 04:47

No, this is not a duplicate of How to detect integer overflow?. The issue is the same but the question is different.


The gcc compiler can optimize away an

4条回答
  •  忘掉有多难
    2021-02-08 05:09

    Ask yourself: how often do you actually need checked arithmetic? If you need it often you should write a checked_int class that overloads the common operators and encapsulate the checks into this class. Props for sharing the implementation on an Open Source website.

    Better yet (arguably), use a big_integer class so that overflows can’t happen in the first place.

提交回复
热议问题