Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
Library Documentation Index

Safe Numerics

PrevUpHomeNext

Checked Arithmetic

Description
Type requirements
Complexity
Example of use
Notes
Synopsis
See Also
Header

Description

Perform binary operations on arithmetic types. Return either a valid result or an error code. Under no circumstances should an incorrect result be returned.

Type requirements

All template parameters of the functions must model Numeric type requirements.

Complexity

Each function performs one and only one arithmetic operation.

Example of use

#include <boost/numeric/safe_numerics/checked_default.hpp>

checked_result<int> r = checked::multiply<int>(24, 42);

Notes

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.

Synopsis

// 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);

See Also

checked_result<R>

Header

#include <boost/numeric/safe_numerics/checked_default.hpp>

#include <boost/numeric/safe_numerics/checked_integer.hpp>

#include <boost/numeric/safe_numerics/checked_float.hpp>


PrevUpHomeNext