Exiv2
Namespaces | Functions
Safe Namespace Reference

Arithmetic operations with overflow checks. More...

Namespaces

 Internal
 Helper structs for providing integer overflow checks.
 

Functions

template<typename T >
add (T summand_1, T summand_2)
 Safe addition, throws an exception on overflow. More...
 

Detailed Description

Arithmetic operations with overflow checks.

Function Documentation

◆ add()

template<typename T >
T Safe::add ( summand_1,
summand_2 
)

Safe addition, throws an exception on overflow.

This function returns the result of summand_1 and summand_2 only when the operation would not overflow, otherwise an exception of type std::overflow_error is thrown.

Parameters
[in]summand_1,summand_2summands to be summed up
Returns
the sum of summand_1 and summand_2
Exceptions
std::overflow_errorif the addition would overflow

This function utilizes compiler builtins when available and should have a very small performance hit then. When builtins are unavailable, a more extensive check is required.

Builtins are available for the following configurations:

  • GCC/Clang for signed and unsigned int, long and long long (not char & short)
  • MSVC for unsigned int, long and long long

Referenced by Safe::Internal::builtin_add_overflow< T >::add(), Exiv2::Jp2Image::readMetadata(), and Exiv2::PsdImage::readMetadata().