• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
3"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
4<section id="safe_numerics.checked_arithmetic">
5  <title>Checked Arithmetic</title>
6
7  <?dbhtml stop-chunking?>
8
9  <section>
10    <title>Description</title>
11
12    <para>Perform binary operations on arithmetic types. Return either a valid
13    result or an error code. Under no circumstances should an incorrect result
14    be returned.</para>
15  </section>
16
17  <section>
18    <title>Type requirements</title>
19
20    <para>All template parameters of the functions must model <link
21    linkend="safe_numerics.integer">Numeric</link> type requirements.</para>
22  </section>
23
24  <section>
25    <title>Complexity</title>
26
27    <para>Each function performs one and only one arithmetic operation.</para>
28  </section>
29
30  <section>
31    <title>Example of use</title>
32
33    <programlisting>#include &lt;boost/numeric/safe_numerics/checked_default.hpp&gt;
34
35checked_result&lt;int&gt; r = checked::multiply&lt;int&gt;(24, 42);
36</programlisting>
37  </section>
38
39  <section>
40    <title>Notes</title>
41
42    <para>Some compilers have command line switches (e.g. -ftrapv) which
43    enable special behavior such that erroneous integer operations are
44    detected at run time. The library has been implemented in such a way that
45    these facilities are not used. It's possible they might be helpful in
46    particular environment. These could be be exploited by re-implementing
47    some functions in this library.</para>
48  </section>
49
50  <section>
51    <title>Synopsis</title>
52
53    <programlisting>// safe casting on primitive types
54template&lt;class R, class T&gt;
55checked_result&lt;R&gt; constexpr checked::cast(const T &amp; t);
56
57// safe addition on primitive types
58template&lt;class R&gt;
59checked_result&lt;R&gt; constexpr checked::add(const R &amp; t, const R &amp; u);
60
61// safe subtraction on primitive types
62template&lt;class R&gt;
63checked_result&lt;R&gt; constexpr checked::subtract(const R &amp; t, const R &amp; u);
64
65// safe multiplication on primitive types
66template&lt;class R&gt;
67checked_result&lt;R&gt; constexpr checked::multiply(const R &amp; t, const R &amp; u);
68
69// safe division on primitive types
70template&lt;class R&gt;
71checked_result&lt;R&gt; constexpr checked::divide(const R &amp; t, const R &amp; u);
72
73// safe modulus on primitive types
74template&lt;class R&gt;
75checked_result&lt;R&gt; constexpr checked::modulus(const R &amp; t, const R &amp; u);
76
77// safe less than predicate on primitive types
78template&lt;class R&gt;
79bool constexpr checked::less_than(const R &amp; t, const R &amp; u);
80
81// safe greater_than_equal predicate on primitive types
82template&lt;class R&gt;
83bool constexpr checked::greater_than_equal(const R &amp; t, const R &amp; u);
84
85// safe greater_than predicate on primitive types
86template&lt;class R&gt;
87bool constexpr checked::greater_than(const R &amp; t, const R &amp; u);
88
89// safe less_than_equal predicate on primitive types
90template&lt;class R&gt;
91bool constexpr checked::less_than_equal(const R &amp; t, const R &amp; u);
92
93// safe equal predicate on primitive types
94template&lt;class R&gt;
95bool constexpr checked::equal(const R &amp; t, const R &amp; u);
96
97// left shift
98template&lt;class R&gt;
99checked_result&lt;R&gt; constexpr checked::left_shift(const R &amp; t, const R &amp; u);
100
101// right shift
102template&lt;class R&gt;
103checked_result&lt;R&gt; constexpr checked::right_shift(const R &amp; t, const R &amp; u);
104
105// bitwise operations
106template&lt;class R&gt;
107checked_result&lt;R&gt; constexpr checked::bitwise_or(const R &amp; t, const R &amp; u);
108
109template&lt;class R&gt;
110checked_result&lt;R&gt; constexpr checked::bitwise_and(const R &amp; t, const R &amp; u);
111
112template&lt;class R&gt;
113checked_result&lt;R&gt; constexpr checked::bitwise_xor(const R &amp; t, const R &amp; u);
114</programlisting>
115  </section>
116
117  <section>
118    <title>See Also</title>
119
120    <para><link
121    linkend="safenumerics.checked_result">checked_result&lt;R&gt;</link></para>
122  </section>
123
124  <section>
125    <title>Header</title>
126
127    <para><ulink
128    url="../../include/boost/safe_numerics/checked_default.hpp"><code>#include
129    &lt;boost/numeric/safe_numerics/checked_default.hpp&gt;
130    </code></ulink></para>
131
132    <para><ulink
133    url="../../include/boost/safe_numerics/checked_integer.hpp"><code>#include
134    &lt;boost/numeric/safe_numerics/checked_integer.hpp&gt;
135    </code></ulink></para>
136
137    <para><ulink url="../../include/checked.hpp"><code/></ulink><ulink
138    url="../../include/boost/safe_numerics/checked_float.hpp"><code>#include
139    &lt;boost/numeric/safe_numerics/checked_float.hpp&gt;
140    </code></ulink></para>
141  </section>
142</section>
143