mirror of
https://github.com/boostorg/safe_numerics.git
synced 2026-02-23 16:02:13 +00:00
128 lines
3.7 KiB
XML
128 lines
3.7 KiB
XML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
|
|
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
|
|
<section id="safe_numerics.checked_integer_arithmetic">
|
|
<title>Checked Integer Arithmetic</title>
|
|
|
|
<?dbhtml stop-chunking?>
|
|
|
|
<section>
|
|
<title>Synopsis</title>
|
|
|
|
<programlisting>// safe casting on primitive types
|
|
template<class R, class T>
|
|
constexpr checked_result<R>
|
|
checked::cast(const T & t);
|
|
|
|
// safe addition on primitive types
|
|
template<class R, class T, class U>
|
|
constexpr checked_result<R>
|
|
checked::add(const T & t, const U & u);
|
|
|
|
// safe subtraction on primitive types
|
|
template<class R, class T, class U>
|
|
constexpr checked_result<R>
|
|
checked::subtract(const T & t, const U & u);
|
|
|
|
// safe multiplication on primitive types
|
|
template<class R, class T, class U>
|
|
constexpr checked_result<R>
|
|
checked::multiply(const T & t, const U & u);
|
|
|
|
// safe division on unsafe types
|
|
template<class R, class T, class U>
|
|
constexpr checked_result<R>
|
|
checked::divide(const T & t, const U & u);
|
|
|
|
template<class R, class T, class U>
|
|
constexpr checked_result<R>
|
|
checked::divide_automatic(const T & t, const U & u);
|
|
|
|
// safe modulus on unsafe types
|
|
template<class R, class T, class U>
|
|
constexpr checked_result<R>
|
|
checked::modulus(const T & t, const U & u);
|
|
|
|
// left shift
|
|
template<class R, class T, class U>
|
|
constexpr checked_result<R>
|
|
checked::left_shift(const T & t, const U & u);
|
|
|
|
// right shift
|
|
template<class R, class T, class U>
|
|
constexpr checked_result<R>
|
|
checked::right_shift(const T & t, const U & u);
|
|
|
|
// bitwise operations
|
|
template<class R, class T, class U>
|
|
constexpr checked_result<R>
|
|
checked::bitwise_or(const T & t, const U & u);
|
|
|
|
template<class R, class T, class U>
|
|
constexpr checked_result<R>
|
|
checked::bitwise_and(const T & t, const U & u);
|
|
|
|
template<class R, class T, class U>
|
|
constexpr checked_result<R>
|
|
checked::bitwise_xor(const T & t, const U & u);
|
|
</programlisting>
|
|
</section>
|
|
|
|
<section>
|
|
<title>Description</title>
|
|
|
|
<para>Perform binary operations on arithmetic types. Return either a valid
|
|
result or an error code. Under no circumstances should an incorrect result
|
|
be returned.</para>
|
|
</section>
|
|
|
|
<section>
|
|
<title>Type requirements</title>
|
|
|
|
<para>All template parameters of the functions must model <link
|
|
linkend="safe_numerics.integer">Integer</link> type requirements.</para>
|
|
</section>
|
|
|
|
<section>
|
|
<title>Complexity</title>
|
|
|
|
<para>Each function performs one and only one arithmetic operation</para>
|
|
</section>
|
|
|
|
<section>
|
|
<title>Header</title>
|
|
|
|
<para><code>#include "checked.hpp" </code></para>
|
|
</section>
|
|
|
|
<section>
|
|
<title>Example of use</title>
|
|
|
|
<para>[A code fragment that illustrates how to use the function.]</para>
|
|
|
|
<programlisting>#include "checked.hpp"
|
|
|
|
checked_result<result_base_type> r = checked::multiply<int>(24, 42);
|
|
</programlisting>
|
|
</section>
|
|
|
|
<section>
|
|
<title>Notes</title>
|
|
|
|
<para>Some compilers have command line switches (e.g. -ftrapv) which
|
|
enable special behavior such erroneous integer operations are detected at
|
|
run time. The library has been implemented in such a way that these
|
|
facilities are not used. It's possible they might be helpful in particular
|
|
environment. These could be be exploited by re-implementing some functions
|
|
in this library.</para>
|
|
</section>
|
|
|
|
<section>
|
|
<title>See Also</title>
|
|
|
|
<para><link
|
|
linkend="safenumerics.checked_result">checked_result<typename
|
|
R></link></para>
|
|
</section>
|
|
</section>
|