Library Documentation Index

Safe Numerics

PrevUpHomeNext

Checked Integer Arithmetic

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

Synopsis

// safe casting on primitive types
template<class R, class T>
constexpr checked_result<R>
checked::cast(const T & t);

// safe addition on primitive types
template<class R, class T, class U>
constexpr checked_result<R>
checked::add(const T & t, const U & u);

// safe subtraction on primitive types
template<class R, class T, class U>
constexpr checked_result<R>
checked::subtract(const T & t, const U & u);

// safe multiplication on primitive types
template<class R, class T, class U>
constexpr checked_result<R>
checked::multiply(const T & t, const U & u);

// safe division on unsafe types
template<class R, class T, class U>
constexpr checked_result<R>
checked::divide(const T & t, const U & u);

template<class R, class T, class U>
constexpr checked_result<R>
checked::divide_automatic(const T & t, const U & u);

// safe modulus on unsafe types
template<class R, class T, class U>
constexpr checked_result<R>
checked::modulus(const T & t, const U & u);

// left shift
template<class R, class T, class U>
constexpr checked_result<R> 
checked::left_shift(const T & t, const U & u);

// right shift
template<class R, class T, class U>
constexpr checked_result<R> 
checked::right_shift(const T & t, const U & u);

// bitwise operations
template<class R, class T, class U>
constexpr checked_result<R> 
checked::bitwise_or(const T & t, const U & u);

template<class R, class T, class U>
constexpr checked_result<R> 
checked::bitwise_and(const T & t, const U & u);

template<class R, class T, class U>
constexpr checked_result<R> 
checked::bitwise_xor(const T & t, const U & u);

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 Integer type requirements.

Complexity

Each function performs one and only one arithmetic operation

Header

#include "checked.hpp"

Example of use

[A code fragment that illustrates how to use the function.]

#include "checked.hpp"

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

Notes

Some compilers have command line switches (e.g. -ftrapv) which enable special behavior such 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.

See Also

checked_result<typename R>


PrevUpHomeNext