• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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&lt;T, U&gt;</title>
6
7  <?dbhtml stop-chunking?>
8
9  <section>
10    <title>Synopsis</title>
11
12    <programlisting>// compare any pair of integers
13template&lt;class T, class U&gt;
14bool constexpr safe_compare::less_than(const T &amp; lhs, const U &amp; rhs);
15
16template&lt;class T, class U&gt;
17bool constexpr safe_compare::greater_than(const T &amp; lhs, const U &amp; rhs);
18
19template&lt;class T, class U&gt;
20bool constexpr safe_compare::less_than_equal(const T &amp; lhs, const U &amp; rhs);
21
22template&lt;class T, class U&gt;
23bool constexpr safe_compare::greater_than_equal(const T &amp; lhs, const U &amp; rhs);
24
25template&lt;class T, class U&gt;
26bool constexpr safe_compare::equal(const T &amp; lhs, const U &amp; rhs);
27
28template&lt;class T, class U&gt;
29bool constexpr safe_compare::not_equal(const T &amp; lhs, const U &amp; 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>&lt;</code>, <code>&gt;</code>, <code><code>&gt;</code>=</code>,
42    <code>&lt;=</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 &lt;cassert&gt;
66#include &lt;safe_compare.hpp&gt;
67
68using namespace boost::numeric;
69const short int x = -64;
70const unsigned int y = 42000;
71
72assert(x &lt; 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    &lt;boost/numeric/safe_numerics/safe_compare.hpp&gt;
82    </code></ulink></para>
83  </section>
84</section>
85