| 1 | <HTML> |
|---|
| 2 | <HEAD> |
|---|
| 3 | <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> |
|---|
| 4 | <LINK REL="stylesheet" TYPE="text/css" HREF="../../../../boost.css"> |
|---|
| 5 | <TITLE>Boost Numeric Conversion Library - Type Requirements and User-defined-types support</TITLE> |
|---|
| 6 | </HEAD> |
|---|
| 7 | <BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000ff" VLINK="#800080"> |
|---|
| 8 | <TABLE BORDER="0" CELLPADDING="7" CELLSPACING="0" WIDTH="100%" |
|---|
| 9 | SUMMARY="header"> |
|---|
| 10 | <TR> |
|---|
| 11 | <TH VALIGN="top" WIDTH="300"> |
|---|
| 12 | <H3><A HREF="../../../../index.htm"><IMG HEIGHT="86" WIDTH="277" |
|---|
| 13 | ALT="C++ Boost" SRC="../../../../boost.png" BORDER="0"></A></H3> </TH> |
|---|
| 14 | <TH VALIGN="top"> |
|---|
| 15 | <H1 ALIGN="center">Boost Numeric Conversion Library</H1> |
|---|
| 16 | <H1 ALIGN="center">Type Requirements<br>and<br>User-defined-types support</H1> |
|---|
| 17 | </TH> |
|---|
| 18 | </TR> |
|---|
| 19 | </TABLE> <HR> |
|---|
| 20 | <H2>Contents</H2> |
|---|
| 21 | <DL CLASS="page-index"> |
|---|
| 22 | <DT><A HREF="#req">Type Requirements</A></DT> |
|---|
| 23 | <DT><A HREF="#sem">UDT's special semantics</A></DT> |
|---|
| 24 | <DT><A HREF="#hooks">Special Policies</A></DT> |
|---|
| 25 | </DL> |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | <HR> |
|---|
| 29 | |
|---|
| 30 | <H2><A NAME="req"></A>Type Requirements</H2> |
|---|
| 31 | <P>Both arithmetic (built-in) and user-defined numeric types require proper specialization of |
|---|
| 32 | <CODE>std::numeric_limits<></CODE> (that is, with (in-class) integral constants).<br> |
|---|
| 33 | The library uses <CODE>std::numeric_limits<T>::is_specialized</CODE> to detect whether |
|---|
| 34 | the type is builtin or user defined, and <CODE>std::numeric_limits<T>::is_integer, |
|---|
| 35 | std::numeric_limits<T>::is_signed</CODE> |
|---|
| 36 | to detect whether the type is integer or floating point; and whether it is signed/unsigned.</P> |
|---|
| 37 | <P>The default Float2IntRounder policies uses unqualified calls to functions <CODE>floor() |
|---|
| 38 | and ceil()</CODE>; but the standard functions are introduced in scope by a |
|---|
| 39 | using directive:</P> |
|---|
| 40 | <PRE>using std::floor ; return floor(s); </PRE> |
|---|
| 41 | <P>Therefore, for builtin arithmetic types, the std functions will be used. |
|---|
| 42 | User defined types should provide overloaded versions of these functions in |
|---|
| 43 | order to use the default rounder policies. If these overloads are defined within a user namespace |
|---|
| 44 | argument dependent lookup (ADL) should find them, but if your compiler has a weak ADL |
|---|
| 45 | you might need to put these functions some place else or write your own rounder policy.</P> |
|---|
| 46 | <P>The default Trunc<> rounder policy needs to determine if the source value |
|---|
| 47 | is positive or not, and for this it evaluates the expression "s < static_cast<S>(0)". |
|---|
| 48 | Therefore, user defined types require a visible operator < in order to use |
|---|
| 49 | the Trunc<> policy (the default).<br> |
|---|
| 50 | </P> |
|---|
| 51 | |
|---|
| 52 | <HR> |
|---|
| 53 | <H2><A NAME="sem"></A>UDT's special semantics</H2> |
|---|
| 54 | |
|---|
| 55 | <p><u>Conversion Traits</u></p> |
|---|
| 56 | <p>If a User Defined Type is involved in a conversion, it is <i>assumed</i> that |
|---|
| 57 | the UDT has <a href="definitions.html#range">wider range</a> than any built-in |
|---|
| 58 | type, and consequently the values of some <code>converter_traits<></code> |
|---|
| 59 | members are hardwired regardless of the reality. The following table summarizes |
|---|
| 60 | this:</p> |
|---|
| 61 | |
|---|
| 62 | <li>Target=UDT and Source=built-in |
|---|
| 63 | <blockquote><code>subranged=false</code><br> |
|---|
| 64 | <code>supertype=Target</code><br> |
|---|
| 65 | <code>subtype=Source</code></blockquote> |
|---|
| 66 | </li> |
|---|
| 67 | |
|---|
| 68 | <li>Target=built-in and Source=UDT |
|---|
| 69 | <blockquote><code>subranged=true</code><br> |
|---|
| 70 | <code>supertype=Source</code><br> |
|---|
| 71 | <code>subtype=Target</code></blockquote> |
|---|
| 72 | </li> |
|---|
| 73 | |
|---|
| 74 | <li>Target=UDT and Source=UDT |
|---|
| 75 | <blockquote><code>subranged=false</code><br> |
|---|
| 76 | <code>supertype=Target</code><br> |
|---|
| 77 | <code>subtype=Source</code></blockquote> |
|---|
| 78 | </li> |
|---|
| 79 | <p>The Traits member <code>udt_mixture</code> can be used to detect whether a |
|---|
| 80 | UDT is involved and to infer the validity of the other members as shown above.</p> |
|---|
| 81 | <p><u>Range Checking</u></p> |
|---|
| 82 | <p>Because User Defined Numeric Types might have peculiar ranges (such as an unbounded |
|---|
| 83 | range), this library does not attempt to supply a meaningful range checking |
|---|
| 84 | logic when UDTs are involved in a conversion. Therefore, if either Target or |
|---|
| 85 | Source are not built-in types, the bundled range checking of the <code>converter<></code> |
|---|
| 86 | function object is automatically disabled. However, it is possible to supply |
|---|
| 87 | a user-defined range-checker. See <A HREF="#hooks">Special Policies</A></p> |
|---|
| 88 | |
|---|
| 89 | <HR> |
|---|
| 90 | <H2><A NAME="hooks"></A>Special Policies</H2> |
|---|
| 91 | <p>There are two components of the <code>converter<></code> class that might |
|---|
| 92 | require special behavior if User Defined Numeric Types are involved: the Range |
|---|
| 93 | Checking and the Raw Conversion.</p> |
|---|
| 94 | <p>When both Target and Source are built-in types, the converter class uses an |
|---|
| 95 | <i>internal</i> range checking logic which is optimized and customized for the |
|---|
| 96 | combined properties of the types.<br> |
|---|
| 97 | However, this internal logic is disabled when either type is User Defined. In |
|---|
| 98 | this case, the user can specify an <i>external</i> range checking policy which |
|---|
| 99 | will be used in place of the internal code. |
|---|
| 100 | See <a HREF="converter_policies.html#rc">UserRangeChecker</a> policy for details.</p> |
|---|
| 101 | <p>The converter class performs the actual conversion using a Raw Converter policy. |
|---|
| 102 | The default raw converter simply performs a <code>"static_cast<Target>(source)".</code><br> |
|---|
| 103 | However, if the a UDT is involved, the static_cast might not work. In this case, |
|---|
| 104 | the user can implement and pass a different raw converter policy. |
|---|
| 105 | See <a HREF="converter_policies.html#rawc">RawConverter</a> policy for details </p> |
|---|
| 106 | <HR> |
|---|
| 107 | <P>Back to <A HREF="index.html">Numeric Conversion library index</A></P> |
|---|
| 108 | <HR> |
|---|
| 109 | <P>Revised 23 June 2004</P> |
|---|
| 110 | <p>© Copyright Fernando Luis Cacciola Carballal, 2004</p> |
|---|
| 111 | <p> Use, modification, and distribution are subject to the Boost Software |
|---|
| 112 | License, Version 1.0. (See accompanying file <a href="../../../../LICENSE_1_0.txt"> |
|---|
| 113 | LICENSE_1_0.txt</a> or copy at <a href="http://www.boost.org/LICENSE_1_0.txt"> |
|---|
| 114 | www.boost.org/LICENSE_1_0.txt</a>)</p> |
|---|
| 115 | </BODY> |
|---|
| 116 | </HTML> |
|---|