-
Notifications
You must be signed in to change notification settings - Fork 264
Closed
Labels
Description
I want to mention this before going ahead:
- I know that behavior of integer division is dependent on computer architectures and operating system.
As the title suggests, this suggestion is about adding runtime checks on integer division.
Since cpp2 is about safety, I think that defining the exact behavior of this operation will be a good thing to have. (In C++, as per standard, integer division by zero is undefined behavior).
I can think of two ways to "define" what will happen, in case there is integer division by zero at runtime:
- Throw an exception: This will be a breaking change because operations on built-in data types are not supposed to throw exceptions in C++ (I am unable to find this guarantee written on cppreference, but it was mentioned in Back to Basics: Exception Handling and Exception Safety, 43:49).
- call
std::terminate()
, which is consistent with "noexcept
" guarantee of built-in data types.
Potential downfalls: This will obviously increase the runtime of program, if the integer division is performed extensively in the program.
The same reasoning is also applicable to integer overflows, but that is a different beast to handle. I am only considering the integer division for this suggestion.
lycogno