safe_numeric_concept.xml 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.safe_numeric_concept">
  5. <title>SafeNumeric&lt;T&gt;</title>
  6. <?dbhtml stop-chunking?>
  7. <section>
  8. <title>Description</title>
  9. <para>This holds an arithmetic value which can be used as a replacement
  10. for built-in C++ arithmetic values. These types differ from their built-in
  11. counter parts in that the are guaranteed not to produce invalid arithmetic
  12. results. These operations return safe types rather than built-in
  13. types.</para>
  14. </section>
  15. <section>
  16. <title>Refinement of</title>
  17. <para><link linkend="safe_numerics.numeric">Numeric</link> or <link
  18. linkend="safe_numerics.integer">Integer</link></para>
  19. </section>
  20. <section>
  21. <title>Notation</title>
  22. <informaltable>
  23. <tgroup cols="2">
  24. <colspec align="left" colwidth="3*"/>
  25. <colspec align="left" colwidth="10*"/>
  26. <thead>
  27. <row>
  28. <entry align="left">Symbol</entry>
  29. <entry align="left">Description</entry>
  30. </row>
  31. </thead>
  32. <tbody>
  33. <row>
  34. <entry><code>T, U</code></entry>
  35. <entry>Types fulfilling <link
  36. linkend="safe_numerics.numeric">Numeric</link> or <link
  37. linkend="safe_numerics.integer">Integer</link> type
  38. requirements.</entry>
  39. </row>
  40. <row>
  41. <entry>t, u</entry>
  42. <entry>objects of types T, U</entry>
  43. </row>
  44. <row>
  45. <entry>S</entry>
  46. <entry>A type fulfilling SafeNumeric type requirements</entry>
  47. </row>
  48. <row>
  49. <entry>s, s1, s2</entry>
  50. <entry>objects of types S</entry>
  51. </row>
  52. <row>
  53. <entry>op</entry>
  54. <entry>C++ infix operator supported by underlying type T</entry>
  55. </row>
  56. <row>
  57. <entry>prefix_op</entry>
  58. <entry>C++ prefix operator: -, +, ~, ++, -- supported by
  59. underlying type T</entry>
  60. </row>
  61. <row>
  62. <entry>postfix_op</entry>
  63. <entry>C++ postfix operator:++, -- supported by underlying type
  64. T</entry>
  65. </row>
  66. <row>
  67. <entry>assign_op</entry>
  68. <entry>C++ assignment operator</entry>
  69. </row>
  70. </tbody>
  71. </tgroup>
  72. </informaltable>
  73. </section>
  74. <section>
  75. <title>Valid Expressions</title>
  76. <para><informaltable>
  77. <tgroup cols="3">
  78. <colspec align="left" colwidth="3*"/>
  79. <colspec align="left" colwidth="2*"/>
  80. <colspec align="left" colwidth="8*"/>
  81. <thead>
  82. <row>
  83. <entry align="left">Expression</entry>
  84. <entry align="left">Result Type</entry>
  85. <entry>Description</entry>
  86. </row>
  87. </thead>
  88. <tbody>
  89. <row>
  90. <entry><code>s op t</code></entry>
  91. <entry>unspecified S</entry>
  92. <entry><para>invoke C++ operator op and return another
  93. SafeNumeric type.</para></entry>
  94. </row>
  95. <row>
  96. <entry><code>t op s</code></entry>
  97. <entry>unspecified S</entry>
  98. <entry><para>invoke C++ operator op and return another
  99. SafeNumeric type.</para></entry>
  100. </row>
  101. <row>
  102. <entry><code>s1 op s2</code></entry>
  103. <entry>unspecified S</entry>
  104. <entry><para>invoke C++ operator op and return another
  105. SafeNumeric type.</para></entry>
  106. </row>
  107. <row>
  108. <entry><code>prefix_op S</code></entry>
  109. <entry>unspecified S</entry>
  110. <entry><para>invoke C++ operator <code>prefix_op</code> and
  111. return another SafeNumeric type.</para></entry>
  112. </row>
  113. <row>
  114. <entry><code>S postfix_op</code></entry>
  115. <entry>unspecified S</entry>
  116. <entry><para>invoke C++ operator <code>postfix_op</code> and
  117. return another SafeNumeric type.</para></entry>
  118. </row>
  119. <row>
  120. <entry><code>s assign_op t</code></entry>
  121. <entry>S &amp;</entry>
  122. <entry><para>convert t to type S and assign it to s.
  123. </para></entry>
  124. </row>
  125. <row>
  126. <entry><code>t assign_op s</code></entry>
  127. <entry>T &amp;</entry>
  128. <entry><para>convert s to type T and assign it to s. If the
  129. value t cannot be represented as an instance of type S, it is an
  130. error.</para></entry>
  131. </row>
  132. <row>
  133. <entry><code>S(t)</code></entry>
  134. <entry>S</entry>
  135. <entry><para>construct an instance of S from a value of type T.
  136. In this case, T is referred to as the base type of S. If the
  137. value t cannot be represented as an instance of type S, it is an
  138. exception condition is invoked. </para></entry>
  139. </row>
  140. <row>
  141. <entry><code>S</code></entry>
  142. <entry>S</entry>
  143. <entry><para>construct an uninitialized instance of
  144. S.</para></entry>
  145. </row>
  146. <row>
  147. <entry><code>T(s)</code></entry>
  148. <entry>T</entry>
  149. <entry><para>implicit conversion of the value of s to type T. If
  150. the value of s cannot be correctly represented as a type T, an
  151. exception condition is invoked.</para></entry>
  152. </row>
  153. <row>
  154. <entry><code>static_cast&lt;T&gt;(s)</code></entry>
  155. <entry>T</entry>
  156. <entry><para>convert the value of s to type T. If the value of s
  157. cannot be correctly represented as a type T, an exception
  158. condition is invoked. </para></entry>
  159. </row>
  160. <row>
  161. <entry><code>is_safe&lt;S&gt;</code></entry>
  162. <entry><code>std::true_type</code></entry>
  163. <entry><para>type trait to query whether any type S fulfills the
  164. requirements for a SafeNumeric type.</para></entry>
  165. </row>
  166. <row>
  167. <entry><code>base_type&lt;S&gt;::type</code></entry>
  168. <entry>T</entry>
  169. <entry><para>Retrieve the base type of a given safe
  170. type.</para></entry>
  171. </row>
  172. <row>
  173. <entry><code>base_value(s)</code></entry>
  174. <entry>T</entry>
  175. <entry><para>Retrieve the value of an instance of a safe type.
  176. This is equivalent to
  177. <code>static_cast&lt;base_type&lt;S&gt;&gt;(s)</code>.</para></entry>
  178. </row>
  179. </tbody>
  180. </tgroup>
  181. </informaltable></para>
  182. <itemizedlist>
  183. <listitem>
  184. <para>The result of any binary operation where one or both of the
  185. operands is a SafeNumeric type is also a SafeNumeric type.</para>
  186. </listitem>
  187. <listitem>
  188. <para>All the expressions in the above table are
  189. <code>constexpr</code> expressions.</para>
  190. </listitem>
  191. <listitem>
  192. <para>Binary expressions which are not assignments and whose operands
  193. are both safe types require that promotion and exception policies of
  194. the operands be identical.</para>
  195. </listitem>
  196. <listitem>
  197. <para><code>Operations on safe types are supported if and only if the
  198. same operation is supported on the underlying types. For example, the
  199. binary operations |</code>, <code>&amp;</code>, <code>^</code> and
  200. <code>~</code> operations defined for safe unsigned integer types. But
  201. they are not defined for floating point types. Currently the are also
  202. defined for signed integer types. It's not clear that this is the
  203. correct decision. On one hand, usage of these operators on signed
  204. types is almost certainly an error in program logic. But trapping this
  205. as an error conflicts with the goal of making safe types "drop-in"
  206. replacements for the corresponding built-in types. In light of this,
  207. these operators are currently supported as they are for normal
  208. built-in types.</para>
  209. </listitem>
  210. <listitem>
  211. <para>Safe Numeric types will be implicitly converted to built-in
  212. types when appropriate. Here's an example:<programlisting>void f(int);
  213. int main(){
  214. long x;
  215. f(x); // OK - builtin implicit version
  216. safe&lt;long&gt; y;
  217. f(y);
  218. return 0;
  219. }</programlisting>This behavior supports the concept of
  220. <code>safe&lt;T&gt;</code> as being a "drop-in" replacement for a
  221. <code>T</code>.</para>
  222. </listitem>
  223. </itemizedlist>
  224. </section>
  225. <section>
  226. <title>Invariants</title>
  227. <para>The fundamental requirement of a SafeNumeric type is that it
  228. implements all C++ operations permitted on its base type in a way the
  229. prevents the return of an incorrect arithmetic result. Various
  230. implementations of this concept may handle circumstances which produce
  231. such results differently (throw exception, compile time trap, etc..). But
  232. no implementation should return an arithmetically incorrect result.</para>
  233. </section>
  234. <section>
  235. <title>Models</title>
  236. <para><code>safe&lt;T&gt;</code></para>
  237. <para><code>safe_signed_range&lt;-11, 11&gt;</code></para>
  238. <para><code>safe_unsigned_range&lt;0, 11&gt;</code></para>
  239. <para><code>safe_signed_literal&lt;4&gt;</code></para>
  240. </section>
  241. <section>
  242. <title>Header</title>
  243. <para><ulink
  244. url="../../include/boost/safe_numerics/concept/safe_numeric.hpp"><code>#include
  245. &lt;boost/numeric/safe_numerics/concepts/safe_numeric.hpp&gt;</code></ulink></para>
  246. </section>
  247. </section>