pre-boost Home Libraries People FAQ More

PrevUpHomeNext

Solution

This library implements special versions of int, unsigned, etc. which behave exactly like the original ones EXCEPT that the results of these operations are guaranteed to be either arithmetically correct or invoke an error. Using this library, the above would be rendered as:

#include <boost/safe_numeric/safe_integer.hpp>

int f(safe<int> x, safe<int> y){
  return x + y; // throw exception if correct result cannot be returned
}

The addition expression is checked at runtime or (if possible) compile time to trap any possible errors resulting from incorrect arithmetic behavior. This will permit one to write arithmetic expressions that cannot produce an erroneous result. Instead, one and only one of the following is guaranteed to occur.

In addition to eliminating undefined behavior from primitive integer types, we define new data types safe_signed_range<MIN, MAX> and safe_unsigned_range<MIN, MAX> which will throw an exception if an attempt is made to store a result which is outside the closed range [MIN, MAX].


PrevUpHomeNext