Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
Library Documentation Index

Safe Numerics

PrevUpHomeNext

safe_compare<T, U>

Synopsis
Description
Type requirements
Complexity
Example of use
Header

Synopsis

// compare any pair of integers
template<class T, class U>
bool constexpr safe_compare::less_than(const T & lhs, const U & rhs);

template<class T, class U>
bool constexpr safe_compare::greater_than(const T & lhs, const U & rhs);

template<class T, class U>
bool constexpr safe_compare::less_than_equal(const T & lhs, const U & rhs);

template<class T, class U>
bool constexpr safe_compare::greater_than_equal(const T & lhs, const U & rhs);

template<class T, class U>
bool constexpr safe_compare::equal(const T & lhs, const U & rhs);

template<class T, class U>
bool constexpr safe_compare::not_equal(const T & lhs, const U & rhs);

Description

Compare two primitive integers. These functions will return a correct result regardless of the type of the operands. Specifically it is guaranteed to return the correct arithmetic result when comparing signed and unsigned types of any size. It does not follow the standard C/C++ procedure of converting the operands to some common type then doing the compare. So it is not equivalent to the C/C++ binary operations <, >, >=, <=, ==, != and shouldn't be used by user programs which should be portable to standard C/C++ integer arithmetic. The functions are free functions defined inside the namespace boost::numeric::safe_compare.

Type requirements

All template parameters of the functions must be C/C++ built-in integer types, char, int ....

Complexity

Each function performs one and only one arithmetic operation.

Example of use

#include <cassert>
#include <safe_compare.hpp>

using namespace boost::numeric;
const short int x = -64;
const unsigned int y = 42000;

assert(x < y); // fails
assert(safe_compare::less_than(x, y)); // OK

Header

#include <boost/numeric/safe_numerics/safe_compare.hpp>


PrevUpHomeNext