1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" 3"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> 4<section id="safe_numerics.checked_integer_arithmetic"> 5 <title>safe_compare<T, U></title> 6 7 <?dbhtml stop-chunking?> 8 9 <section> 10 <title>Synopsis</title> 11 12 <programlisting>// compare any pair of integers 13template<class T, class U> 14bool constexpr safe_compare::less_than(const T & lhs, const U & rhs); 15 16template<class T, class U> 17bool constexpr safe_compare::greater_than(const T & lhs, const U & rhs); 18 19template<class T, class U> 20bool constexpr safe_compare::less_than_equal(const T & lhs, const U & rhs); 21 22template<class T, class U> 23bool constexpr safe_compare::greater_than_equal(const T & lhs, const U & rhs); 24 25template<class T, class U> 26bool constexpr safe_compare::equal(const T & lhs, const U & rhs); 27 28template<class T, class U> 29bool constexpr safe_compare::not_equal(const T & lhs, const U & rhs);</programlisting> 30 </section> 31 32 <section> 33 <title>Description</title> 34 35 <para>Compare two primitive integers. These functions will return a 36 correct result regardless of the type of the operands. Specifically it is 37 guaranteed to return the correct arithmetic result when comparing signed 38 and unsigned types of any size. It does not follow the standard C/C++ 39 procedure of converting the operands to some common type then doing the 40 compare. So it is not equivalent to the C/C++ binary operations 41 <code><</code>, <code>></code>, <code><code>></code>=</code>, 42 <code><=</code>, <code>==</code>, <code>!=</code> and shouldn't be used 43 by user programs which should be portable to standard C/C++ integer 44 arithmetic. The functions are free functions defined inside the namespace 45 <code>boost::numeric::safe_compare</code>.</para> 46 </section> 47 48 <section> 49 <title>Type requirements</title> 50 51 <para>All template parameters of the functions must be C/C++ built-in 52 integer types, <code><code>char</code></code>, 53 <code><code>int</code></code> ....</para> 54 </section> 55 56 <section> 57 <title>Complexity</title> 58 59 <para>Each function performs one and only one arithmetic operation.</para> 60 </section> 61 62 <section> 63 <title>Example of use</title> 64 65 <programlisting>#include <cassert> 66#include <safe_compare.hpp> 67 68using namespace boost::numeric; 69const short int x = -64; 70const unsigned int y = 42000; 71 72assert(x < y); // fails 73assert(safe_compare::less_than(x, y)); // OK</programlisting> 74 </section> 75 76 <section> 77 <title>Header</title> 78 79 <para><ulink 80 url="../../include/boost/safe_numerics/safe_compare.hpp"><code>#include 81 <boost/numeric/safe_numerics/safe_compare.hpp> 82 </code></ulink></para> 83 </section> 84</section> 85