safe_compare.xml 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. <?dbhtml stop-chunking?>
  7. <section>
  8. <title>Synopsis</title>
  9. <programlisting>// compare any pair of integers
  10. template&lt;class T, class U&gt;
  11. bool constexpr safe_compare::less_than(const T &amp; lhs, const U &amp; rhs);
  12. template&lt;class T, class U&gt;
  13. bool constexpr safe_compare::greater_than(const T &amp; lhs, const U &amp; rhs);
  14. template&lt;class T, class U&gt;
  15. bool constexpr safe_compare::less_than_equal(const T &amp; lhs, const U &amp; rhs);
  16. template&lt;class T, class U&gt;
  17. bool constexpr safe_compare::greater_than_equal(const T &amp; lhs, const U &amp; rhs);
  18. template&lt;class T, class U&gt;
  19. bool constexpr safe_compare::equal(const T &amp; lhs, const U &amp; rhs);
  20. template&lt;class T, class U&gt;
  21. bool constexpr safe_compare::not_equal(const T &amp; lhs, const U &amp; rhs);</programlisting>
  22. </section>
  23. <section>
  24. <title>Description</title>
  25. <para>Compare two primitive integers. These functions will return a
  26. correct result regardless of the type of the operands. Specifically it is
  27. guaranteed to return the correct arithmetic result when comparing signed
  28. and unsigned types of any size. It does not follow the standard C/C++
  29. procedure of converting the operands to some common type then doing the
  30. compare. So it is not equivalent to the C/C++ binary operations
  31. <code>&lt;</code>, <code>&gt;</code>, <code><code>&gt;</code>=</code>,
  32. <code>&lt;=</code>, <code>==</code>, <code>!=</code> and shouldn't be used
  33. by user programs which should be portable to standard C/C++ integer
  34. arithmetic. The functions are free functions defined inside the namespace
  35. <code>boost::numeric::safe_compare</code>.</para>
  36. </section>
  37. <section>
  38. <title>Type requirements</title>
  39. <para>All template parameters of the functions must be C/C++ built-in
  40. integer types, <code><code>char</code></code>,
  41. <code><code>int</code></code> ....</para>
  42. </section>
  43. <section>
  44. <title>Complexity</title>
  45. <para>Each function performs one and only one arithmetic operation.</para>
  46. </section>
  47. <section>
  48. <title>Example of use</title>
  49. <programlisting>#include &lt;cassert&gt;
  50. #include &lt;safe_compare.hpp&gt;
  51. using namespace boost::numeric;
  52. const short int x = -64;
  53. const unsigned int y = 42000;
  54. assert(x &lt; y); // fails
  55. assert(safe_compare::less_than(x, y)); // OK</programlisting>
  56. </section>
  57. <section>
  58. <title>Header</title>
  59. <para><ulink
  60. url="../../include/boost/safe_numerics/safe_compare.hpp"><code>#include
  61. &lt;boost/numeric/safe_numerics/safe_compare.hpp&gt;
  62. </code></ulink></para>
  63. </section>
  64. </section>