description | title | ms.date | ms.topic | f1_keywords | helpviewer_keywords | ms.assetid | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: SafeInt Functions |
SafeInt Functions |
06/23/2020 |
reference |
|
|
fdc208e5-5d8a-41a9-8271-567fd438958d |
The SafeInt library provides several functions that you can use without creating an instance of the SafeInt class. If you want to protect a single mathematical operation from integer overflow, you can use these functions. If you want to protect multiple mathematical operations, you should create SafeInt
objects. It's more efficient to create SafeInt
objects than to use these functions multiple times.
These functions enable you to compare or perform mathematical operations on two different types of parameters without having to convert them to the same type first.
Each of these functions has two template types: T
and U
. Each of these types can be a Boolean, character, or integral type. Integral types can be signed or unsigned and any size from 8 bits to 64 bits.
Note
The latest version of this library is located at https://github.com/dcleblanc/SafeInt.
Function | Description |
---|---|
SafeAdd | Adds two numbers and protects against overflow. |
SafeCast | Casts one type of parameter to another type. |
SafeDivide | Divides two numbers and protects against dividing by zero. |
SafeEquals, SafeGreaterThan, SafeGreaterThanEquals, SafeLessThan, SafeLessThanEquals, SafeNotEquals | Compares two numbers. These functions enable you to compare two different types of numbers without changing their types. |
SafeModulus | Performs the modulus operation on two numbers. |
SafeMultiply | Multiplies two numbers together and protects against overflow. |
SafeSubtract | Subtracts two numbers and protects against overflow. |
Section | Description |
---|---|
SafeInt | The SafeInt class. |
SafeIntException | The exception class specific to the SafeInt library. |
Adds two numbers in a way that protects against overflow.
template<typename T, typename U>
inline bool SafeAdd (
T t,
U u,
T& result
) throw ();
t
[in] The first number to add. This must be of type T.
u
[in] The second number to add. This must be of type U.
result
[out] The parameter where SafeAdd
stores the result.
true
if no error occurs; false
if an error occurs.
Casts one type of number to another type.
template<typename T, typename U>
inline bool SafeCast (
const T From,
U& To
);
From
[in] The source number to convert. This must be of type T
.
To
[out] A reference to the new number type. This must be of type U
.
true
if no error occurs; false
if an error occurs.
Divides two numbers in a way that protects against dividing by zero.
template<typename T, typename U>
inline bool SafeDivide (
T t,
U u,
T& result
) throw ();
t
[in] The dividend. This must be of type T.
u
[in] The divisor. This must be of type U.
result
[out] The parameter where SafeDivide
stores the result.
true
if no error occurs; false
if an error occurs.
Compares two numbers to determine whether they're equal.
template<typename T, typename U>
inline bool SafeEquals (
const T t,
const U u
) throw ();
t
[in] The first number to compare. This must be of type T.
u
[in] The second number to compare. This must be of type U.
true
if t and u are equal; otherwise false
.
The method enhances ==
because SafeEquals
enables you to compare two different types of numbers.
Compares two numbers.
template<typename T, typename U>
inline bool SafeGreaterThan (
const T t,
const U u
) throw ();
t
[in] The first number to compare. This must be of type T
.
u
[in] The second number to compare. This must be of type U
.
true
if t is greater than u; otherwise false
.
SafeGreaterThan
extends the regular comparison operator by enabling you to compare two different types of numbers.
Compares two numbers.
template <typename T, typename U>
inline bool SafeGreaterThanEquals (
const T t,
const U u
) throw ();
t
[in] The first number to compare. This must be of type T
.
u
[in] The second number to compare. This must be of type U
.
true
if t is greater than or equal to u; otherwise false
.
SafeGreaterThanEquals
enhances the standard comparison operator because it enables you to compare two different types of numbers.
Determines whether one number is less than another.
template<typename T, typename U>
inline bool SafeLessThan (
const T t,
const U u
) throw ();
t
[in] The first number. This must be of type T
.
u
[in] The second number. This must be of type U
.
true
if t is less than u; otherwise false
.
This method enhances the standard comparison operator because SafeLessThan
enables you to compare two different types of number.
Compares two numbers.
template <typename T, typename U>
inline bool SafeLessThanEquals (
const T t,
const U u
) throw ();
t
[in] The first number to compare. This must be of type T
.
u
[in] The second number to compare. This must be of type U
.
true
if t is less than or equal to u; otherwise false
.
SafeLessThanEquals
extends the regular comparison operator by enabling you to compare two different types of numbers.
Performs the modulus operation on two numbers.
template<typename T, typename U>
inline bool SafeModulus (
const T t,
const U u,
T& result
) throw ();
t
[in] The divisor. This must be of type T
.
u
[in] The dividend. This must be of type U
.
result
[out] The parameter where SafeModulus
stores the result.
true
if no error occurs; false
if an error occurs.
Multiplies two numbers together in a way that protects against overflow.
template<typename T, typename U>
inline bool SafeMultiply (
T t,
U u,
T& result
) throw ();
t
[in] The first number to multiply. This must be of type T
.
u
[in] The second number to multiply. This must be of type U
.
result
[out] The parameter where SafeMultiply
stores the result.
true
if no error occurs; false
if an error occurs.
Determines if two numbers aren't equal.
template<typename T, typename U>
inline bool SafeNotEquals (
const T t,
const U u
) throw ();
t
[in] The first number to compare. This must be of type T
.
u
[in] The second number to compare. This must be of type U
.
true
if t and u aren't equal; otherwise false
.
The method enhances !=
because SafeNotEquals
enables you to compare two different types of numbers.
Subtracts two numbers in a way that protects against overflow.
template<typename T, typename U>
inline bool SafeSubtract (
T t,
U u,
T& result
) throw ();
t
[in] The first number in the subtraction. This must be of type T
.
u
[in] The number to subtract from t. This must be of type U
.
result
[out] The parameter where SafeSubtract
stores the result.
true
if no error occurs; false
if an error occurs.