pre-boost Home Libraries People FAQ More

PrevUpHomeNext

Functions

safe_cast<T, U>
Synopsis
Description
Type requirements
Preconditions
Complexity
Header
Example of use
safe_compare<T, U>
Synopsis
Description
Type requirements
Header
Example of use
overflow
Synopsis
Description
Header
Example of use
See Also

safe_cast<T, U>

Synopsis

template<class T, class U>
T safe_cast(const U & u);

Description

Converts one Numeric type to another. Throws an std::out_of_range exception if such a conversion is not possible without changing the value.

Type requirements

Type Requirements
T Numeric
U Numeric

Preconditions

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.

Complexity

Constant - O(0).

Example of use

#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<T, U>

Synopsis

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

Description

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.

Type requirements

Type Requirements
T Numeric
U Numeric

Example of use

#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
}

overflow

Synopsis

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

Description

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.

Example of use

#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 Also

See rationale for more information on this function


PrevUpHomeNext