pre-boost Home Libraries People FAQ More

PrevUpHomeNext

safe<T>

Description
Template Parameters
Model of
Derived From
Notation
Header
Example of use
Notes
See Also

Description

A safe<T> can be used anywhere a type T is used. When T is used in operation which overflows, a exception is thrown

Template Parameters

Parameter Type Requirements Description
T Numeric

The underlying integer type

Model of

Numeric

Derived From

safe_signed_range<T> (if std::numeric_limits<T>::is_signed == ftrue)

safe_unsigned_range<T> (if std::numeric_limits<T>::is_signed == ftrue)

Notation

Symbol Description
T A type that models the Numeric concept
t An object of type T

Header

#include <boost/safe_numerics/safe_integer.hpp>

Example of use

The following program will emit an error message on a machine where int is only 16 bits but run without problem on a machine where int is 32 bits.

#include <boost/numeric/safe.hpp>
#include <iostream>
void f(){
    safe<int> i = 400;
    safe<int> j;
    try {
        j = i * i;
    }
    catch(...){
       std::cout << "overflow error" << std::endl;
    }
}

Notes

Footnotes (if any) that are referred to by other parts of the page.

See Also

Footnotes (if any) that are referred to by other parts of the page.


PrevUpHomeNext