...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Safe Numerics |
Perform binary operations on arithmetic types. Return either a valid result or an error code. Under no circumstances should an incorrect result be returned.
All template parameters of the functions must model Numeric type requirements.
#include <boost/numeric/safe_numerics/checked_default.hpp> checked_result<int> r = checked::multiply<int>(24, 42);
Some compilers have command line switches (e.g. -ftrapv) which enable special behavior such that erroneous integer operations are detected at run time. The library has been implemented in such a way that these facilities are not used. It's possible they might be helpful in particular environment. These could be be exploited by re-implementing some functions in this library.
// safe casting on primitive types template<class R, class T> checked_result<R> constexpr checked::cast(const T & t); // safe addition on primitive types template<class R> checked_result<R> constexpr checked::add(const R & t, const R & u); // safe subtraction on primitive types template<class R> checked_result<R> constexpr checked::subtract(const R & t, const R & u); // safe multiplication on primitive types template<class R> checked_result<R> constexpr checked::multiply(const R & t, const R & u); // safe division on primitive types template<class R> checked_result<R> constexpr checked::divide(const R & t, const R & u); // safe modulus on primitive types template<class R> checked_result<R> constexpr checked::modulus(const R & t, const R & u); // safe less than predicate on primitive types template<class R> bool constexpr checked::less_than(const R & t, const R & u); // safe greater_than_equal predicate on primitive types template<class R> bool constexpr checked::greater_than_equal(const R & t, const R & u); // safe greater_than predicate on primitive types template<class R> bool constexpr checked::greater_than(const R & t, const R & u); // safe less_than_equal predicate on primitive types template<class R> bool constexpr checked::less_than_equal(const R & t, const R & u); // safe equal predicate on primitive types template<class R> bool constexpr checked::equal(const R & t, const R & u); // left shift template<class R> checked_result<R> constexpr checked::left_shift(const R & t, const R & u); // right shift template<class R> checked_result<R> constexpr checked::right_shift(const R & t, const R & u); // bitwise operations template<class R> checked_result<R> constexpr checked::bitwise_or(const R & t, const R & u); template<class R> checked_result<R> constexpr checked::bitwise_and(const R & t, const R & u); template<class R> checked_result<R> constexpr checked::bitwise_xor(const R & t, const R & u);