![]() |
Home | Libraries | People | FAQ | More |
A safe<T> can be used anywhere a type T is used.
When T is used in operation which overflows, a exception is thrown
| Parameter | Type Requirements | Description |
|---|---|---|
T |
Numeric | The underlying integer type |
safe_signed_range<T>
(if std::numeric_limits<T>::is_signed == ftrue)
safe_unsigned_range<T>
(if std::numeric_limits<T>::is_signed == ftrue)
| Symbol | Description |
|---|---|
T |
A type that models the Numeric concept |
| t | An object of type T
|
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;
}
}