1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?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.safe_cast">
- <title>safe_cast<T, U></title>
- <section>
- <title>Synopsis</title>
- <programlisting>template<class T, class U>
- T safe_cast(const U & u);</programlisting>
- </section>
- <section>
- <title>Description</title>
- <para>Converts one <link linkend="safe_numerics.numeric">Numeric</link>
- type to another. Throws an <code>std::out_of_range</code> exception if
- such a conversion is not possible without changing the value. This
- function is part of the implementation of the safe numerics library. It's
- been made publicly because it might be useful in related contexts.</para>
- </section>
- <section>
- <title>Type requirements</title>
- <informaltable>
- <tgroup cols="2">
- <colspec align="left"/>
- <colspec align="left" colwidth="3*"/>
- <thead>
- <row>
- <entry align="left">Type</entry>
- <entry align="left">Requirements</entry>
- </row>
- </thead>
- <tbody>
- <row>
- <entry><code>T</code></entry>
- <entry><link
- linkend="safe_numerics.numeric">Numeric</link></entry>
- </row>
- <row>
- <entry><code>U </code></entry>
- <entry><link
- linkend="safe_numerics.numeric">Numeric</link></entry>
- </row>
- </tbody>
- </tgroup>
- </informaltable>
- </section>
- <section>
- <title>Preconditions</title>
- <para>The value of u must be representable by the type <code>T</code>. If
- this is not true, an <code>std::out_of_range</code> exception will be
- thrown.</para>
- </section>
- <section>
- <title>Header</title>
- <para><filename><ulink url="../../include/safe_cast.hpp">#include
- <boost/numeric/safe_cast.hpp> </ulink></filename></para>
- </section>
- <section>
- <title>Example of use</title>
- <programlisting>#include <boost/numeric/safe_cast.hpp>
- #include <boost/numeric/safe_integer.hpp>
- void f(){
- safe_integer<char> i;
- unsigned char j;
- i = 1;
- j = safe_cast<unsigned char>(i); // ok
- i = -1;
- j = safe_cast<unsigned char>(i); // throws std::out_of_range exception
- i = 1024;
- j = safe_cast<unsigned char>(i); // throws std::out_of_range exception
- }</programlisting>
- </section>
- </section>
|