Skip to content

[SUGGESTION] Add runtime checks for C++ integer division by zero #1184

@nullicist-aakash

Description

@nullicist-aakash

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions