![]() |
Home | Libraries | People | FAQ | More |
Converts one Numeric
type to another. Throws an std::out_of_range exception if
such a conversion is not possible without changing the value.
The value of u must be representabl by the type T. If
this is not true, an std::out_of_range exception will be
thrown.
#include <boost/numeric/safe_cast.hpp>
#include <boost/numeric/safe_integer.hpp>
void f(){
safe_integer<char> i;
unsigned char j;
i = 1;
j = safe_cast<unsigned char>(i); // ok
i = -1;
j = safe_cast<unsigned char>(i); // throws std::out_of_range exception
i = 1024;
j = safe_cast<unsigned char>(i); // throws std::out_of_range exception
}
safe_compare is several functions:.
template<class T, class U> bool safe_compare::less_than(const T & lhs, const U & rhs); template<class T, class U> bool safe_compare::less_than_equal(const T & lhs, const U & rhs); template<class T, class U> bool safe_compare::greater_than(const T & lhs, const U & rhs); template<class T, class U> bool safe_compare::greater_than_equal(const T & lhs, const U & rhs); template<class T, class U> bool safe_compare::equal(const T & lhs, const U & rhs); template<class T, class U> bool safe_compare::not_equal(const T & lhs, const U & rhs);
With normal comparison operators, comparison of unsigned types to signed types will be done by converting the unsigned type to a signed type before comparing. Unfortunately this is not always possible. Most C++ compilers will emit an warning message when this is possible but won't check that an error is made in the conversion. This function guarentees a correct result regardless of the types of the arguments.
#include <boost/numeric/safe_compare.hpp>
void f(){
unsigned char i = 129;
unsigned int j = 1;
assert(j < i); // program traps because expression is false. But this is a surprise because 1 < 129
assert( safe_compare::less_than(j,i)); // expression is true as we would expect
}
// safe_compare is used to implement comparison operators for safe types. So we can do this:
void g(){
safe<unsigned char> int i = 0x129;
safe<int> j = 1;
assert(j < i); // program works as expected
}
This function is invoked by the library whenever it is not possible to produce a result for an arithmetic operation.
void overflow(char const * const msg);
If evironment supports C++ exceptions, this function throws the exception .
If the environment does not support C++ exceptions, the user should
implement this function and expect it to be called when appropriate.
Otherwise, function is implemented by the library so that it throws the
standard library exception std::out_of_range(msg).
boost/config.hpp defines BOOST_NO_EXCEPTIONS
when the environment doesn't support exceptions. It is by checking for the
definition of this macro that the system determines whether or not
exceptions are supported.
#include <cstdio>
void overflow(char const * const msg){
std::fputs("safe_numerics overflow error:, std::stderr);
std::fputs(msg, std::stderr);
std::fputc('\n', std::stderr);
}
See rationale for more information on this function