Edit

Share via


SafeInt Functions

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.

In This Section

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.

SafeAdd

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

Parameters

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.

Return Value

true if no error occurs; false if an error occurs.

SafeCast

Casts one type of number to another type.

template<typename T, typename U>
inline bool SafeCast (
   const T From,
   U& To
);

Parameters

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.

Return Value

true if no error occurs; false if an error occurs.

SafeDivide

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

Parameters

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.

Return Value

true if no error occurs; false if an error occurs.

SafeEquals

Compares two numbers to determine whether they're equal.

template<typename T, typename U>
inline bool SafeEquals (
   const T t,
   const U u
) throw ();

Parameters

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.

Return Value

true if t and u are equal; otherwise false.

Remarks

The method enhances == because SafeEquals enables you to compare two different types of numbers.

SafeGreaterThan

Compares two numbers.

template<typename T, typename U>
inline bool SafeGreaterThan (
   const T t,
   const U u
) throw ();

Parameters

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.

Return Value

true if t is greater than u; otherwise false.

Remarks

SafeGreaterThan extends the regular comparison operator by enabling you to compare two different types of numbers.

SafeGreaterThanEquals

Compares two numbers.

template <typename T, typename U>
inline bool SafeGreaterThanEquals (
   const T t,
   const U u
) throw ();

Parameters

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.

Return Value

true if t is greater than or equal to u; otherwise false.

Remarks

SafeGreaterThanEquals enhances the standard comparison operator because it enables you to compare two different types of numbers.

SafeLessThan

Determines whether one number is less than another.

template<typename T, typename U>
inline bool SafeLessThan (
   const T t,
   const U u
) throw ();

Parameters

t
[in] The first number. This must be of type T.

u
[in] The second number. This must be of type U.

Return Value

true if t is less than u; otherwise false.

Remarks

This method enhances the standard comparison operator because SafeLessThan enables you to compare two different types of number.

SafeLessThanEquals

Compares two numbers.

template <typename T, typename U>
inline bool SafeLessThanEquals (
   const T t,
   const U u
) throw ();

Parameters

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.

Return Value

true if t is less than or equal to u; otherwise false.

Remarks

SafeLessThanEquals extends the regular comparison operator by enabling you to compare two different types of numbers.

SafeModulus

Performs the modulus operation on two numbers.

template<typename T, typename U>
inline bool SafeModulus (
   const T t,
   const U u,
   T& result
) throw ();

Parameters

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.

Return Value

true if no error occurs; false if an error occurs.

SafeMultiply

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

Parameters

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.

Return Value

true if no error occurs; false if an error occurs.

SafeNotEquals

Determines if two numbers aren't equal.

template<typename T, typename U>
inline bool SafeNotEquals (
   const T t,
   const U u
) throw ();

Parameters

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.

Return Value

true if t and u aren't equal; otherwise false.

Remarks

The method enhances != because SafeNotEquals enables you to compare two different types of numbers.

SafeSubtract

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

Parameters

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.

Return Value

true if no error occurs; false if an error occurs.