blob: 65a6ba4aa417dea4049b8110ea1df0613279aec9 [file] [log] [blame]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Type Requirements and User-defined-types support</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="../index.html" title="Chapter 1. Boost.NumericConversion">
<link rel="up" href="../index.html" title="Chapter 1. Boost.NumericConversion">
<link rel="prev" href="converter___function_object.html" title="converter&lt;&gt; function object">
<link rel="next" href="bounds___traits_class.html" title="bounds&lt;&gt; traits class">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="converter___function_object.html"><img src="../../../../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="bounds___traits_class.html"><img src="../../../../../../doc/html/images/next.png" alt="Next"></a>
</div>
<div class="section" title="Type Requirements and User-defined-types support">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_numericconversion.type_requirements_and_user_defined_types_support"></a><a class="link" href="type_requirements_and_user_defined_types_support.html" title="Type Requirements and User-defined-types support">Type
Requirements and User-defined-types support</a>
</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="type_requirements_and_user_defined_types_support.html#boost_numericconversion.type_requirements_and_user_defined_types_support.type_requirements">Type
Requirements</a></span></dt>
<dt><span class="section"><a href="type_requirements_and_user_defined_types_support.html#boost_numericconversion.type_requirements_and_user_defined_types_support.udt_s_special_semantics">UDT's
special semantics</a></span></dt>
<dt><span class="section"><a href="type_requirements_and_user_defined_types_support.html#boost_numericconversion.type_requirements_and_user_defined_types_support.special_policies">Special
Policies</a></span></dt>
</dl></div>
<div class="section" title="Type Requirements">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_numericconversion.type_requirements_and_user_defined_types_support.type_requirements"></a><a class="link" href="type_requirements_and_user_defined_types_support.html#boost_numericconversion.type_requirements_and_user_defined_types_support.type_requirements" title="Type Requirements">Type
Requirements</a>
</h3></div></div></div>
<p>
Both arithmetic (built-in) and user-defined numeric types require proper
specialization of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;&gt;</span></code>
(that is, with (in-class) integral constants).
</p>
<p>
The library uses <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">is_specialized</span></code> to detect whether the type
is builtin or user defined, and <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">is_integer</span></code>, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">is_signed</span></code> to detect whether the type is
integer or floating point; and whether it is signed/unsigned.
</p>
<p>
The default <code class="computeroutput"><span class="identifier">Float2IntRounder</span></code>
policies uses unqualified calls to functions <code class="computeroutput"><span class="identifier">floor</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">ceil</span><span class="special">()</span></code>; but the standard functions are introduced
in scope by a using directive:
</p>
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">floor</span> <span class="special">;</span> <span class="keyword">return</span> <span class="identifier">floor</span><span class="special">(</span><span class="identifier">s</span><span class="special">);</span>
</pre>
<p>
Therefore, for builtin arithmetic types, the std functions will be used.
User defined types should provide overloaded versions of these functions
in order to use the default rounder policies. If these overloads are defined
within a user namespace argument dependent lookup (ADL) should find them,
but if your compiler has a weak ADL you might need to put these functions
some place else or write your own rounder policy.
</p>
<p>
The default <code class="computeroutput"><span class="identifier">Trunc</span><span class="special">&lt;&gt;</span></code>
rounder policy needs to determine if the source value is positive or not,
and for this it evaluates the expression <code class="computeroutput"><span class="identifier">s</span>
<span class="special">&lt;</span> <span class="keyword">static_cast</span><span class="special">&lt;</span><span class="identifier">S</span><span class="special">&gt;(</span><span class="number">0</span><span class="special">)</span></code>. Therefore,
user defined types require a visible <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;</span></code> in order to use the <code class="computeroutput"><span class="identifier">Trunc</span><span class="special">&lt;&gt;</span></code> policy (the default).
</p>
</div>
<div class="section" title="UDT's special semantics">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_numericconversion.type_requirements_and_user_defined_types_support.udt_s_special_semantics"></a><a class="link" href="type_requirements_and_user_defined_types_support.html#boost_numericconversion.type_requirements_and_user_defined_types_support.udt_s_special_semantics" title="UDT's special semantics">UDT's
special semantics</a>
</h3></div></div></div>
<a name="boost_numericconversion.type_requirements_and_user_defined_types_support.udt_s_special_semantics.conversion_traits"></a><h5>
<a name="id564459"></a>
<a class="link" href="type_requirements_and_user_defined_types_support.html#boost_numericconversion.type_requirements_and_user_defined_types_support.udt_s_special_semantics.conversion_traits">Conversion
Traits</a>
</h5>
<p>
If a User Defined Type is involved in a conversion, it is <span class="emphasis"><em>assumed</em></span>
that the UDT has <a class="link" href="definitions.html#boost_numericconversion.definitions.range_and_precision" title="Range and Precision">wider
range</a> than any built-in type, and consequently the values of some
<code class="computeroutput"><span class="identifier">converter_traits</span><span class="special">&lt;&gt;</span></code>
members are hardwired regardless of the reality. The following table summarizes
this:
</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
<code class="computeroutput"><span class="identifier">Target</span><span class="special">=</span></code><span class="emphasis"><em>UDT</em></span>
and <code class="computeroutput"><span class="identifier">Source</span><span class="special">=</span></code><span class="emphasis"><em>built-in</em></span><div class="itemizedlist"><ul class="itemizedlist" type="circle">
<li class="listitem"><code class="computeroutput"><span class="identifier">subranged</span><span class="special">=</span><span class="keyword">false</span></code></li>
<li class="listitem"><code class="computeroutput"><span class="identifier">supertype</span><span class="special">=</span><span class="identifier">Target</span></code></li>
<li class="listitem"><code class="computeroutput"><span class="identifier">subtype</span><span class="special">=</span><span class="identifier">Source</span></code></li>
</ul></div>
</li>
<li class="listitem">
<code class="computeroutput"><span class="identifier">Target</span><span class="special">=</span></code><span class="emphasis"><em>built-in</em></span>
and <code class="computeroutput"><span class="identifier">Source</span><span class="special">=</span></code><span class="emphasis"><em>UDT</em></span><div class="itemizedlist"><ul class="itemizedlist" type="circle">
<li class="listitem"><code class="computeroutput"><span class="identifier">subranged</span><span class="special">=</span><span class="keyword">true</span></code></li>
<li class="listitem"><code class="computeroutput"><span class="identifier">supertype</span><span class="special">=</span><span class="identifier">Source</span></code></li>
<li class="listitem"><code class="computeroutput"><span class="identifier">subtype</span><span class="special">=</span><span class="identifier">Target</span></code></li>
</ul></div>
</li>
<li class="listitem">
<code class="computeroutput"><span class="identifier">Target</span><span class="special">=</span></code><span class="emphasis"><em>UDT</em></span>
and <code class="computeroutput"><span class="identifier">Source</span><span class="special">=</span></code><span class="emphasis"><em>UDT</em></span><div class="itemizedlist"><ul class="itemizedlist" type="circle">
<li class="listitem"><code class="computeroutput"><span class="identifier">subranged</span><span class="special">=</span><span class="keyword">false</span></code></li>
<li class="listitem"><code class="computeroutput"><span class="identifier">supertype</span><span class="special">=</span><span class="identifier">Target</span></code></li>
<li class="listitem"><code class="computeroutput"><span class="identifier">subtype</span><span class="special">=</span><span class="identifier">Source</span></code></li>
</ul></div>
</li>
</ul></div>
<p>
The <code class="computeroutput"><span class="identifier">Traits</span></code> member <code class="computeroutput"><span class="identifier">udt_mixture</span></code> can be used to detect whether
a UDT is involved and to infer the validity of the other members as shown
above.
</p>
<a name="boost_numericconversion.type_requirements_and_user_defined_types_support.udt_s_special_semantics.range_checking"></a><h5>
<a name="id564867"></a>
<a class="link" href="type_requirements_and_user_defined_types_support.html#boost_numericconversion.type_requirements_and_user_defined_types_support.udt_s_special_semantics.range_checking">Range
Checking</a>
</h5>
<p>
Because User Defined Numeric Types might have peculiar ranges (such as an
unbounded range), this library does not attempt to supply a meaningful range
checking logic when UDTs are involved in a conversion. Therefore, if either
Target or Source are not built-in types, the bundled range checking of the
<code class="computeroutput"><span class="identifier">converter</span><span class="special">&lt;&gt;</span></code>
function object is automatically disabled. However, it is possible to supply
a user-defined range-checker. See <a class="link" href="type_requirements_and_user_defined_types_support.html#boost_numericconversion.type_requirements_and_user_defined_types_support.special_policies" title="Special Policies">Special
Policies</a>
</p>
</div>
<div class="section" title="Special Policies">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_numericconversion.type_requirements_and_user_defined_types_support.special_policies"></a><a class="link" href="type_requirements_and_user_defined_types_support.html#boost_numericconversion.type_requirements_and_user_defined_types_support.special_policies" title="Special Policies">Special
Policies</a>
</h3></div></div></div>
<p>
There are two components of the <code class="computeroutput"><span class="identifier">converter</span><span class="special">&lt;&gt;</span></code> class that might require special
behavior if User Defined Numeric Types are involved: the Range Checking and
the Raw Conversion.
</p>
<p>
When both Target and Source are built-in types, the converter class uses
an internal range checking logic which is optimized and customized for the
combined properties of the types.
</p>
<p>
However, this internal logic is disabled when either type is User Defined.
In this case, the user can specify an <span class="emphasis"><em>external</em></span> range
checking policy which will be used in place of the internal code. See <a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_userrangechecker" title="Policy UserRangeChecker">UserRangeChecker</a>
policy for details.
</p>
<p>
The converter class performs the actual conversion using a Raw Converter
policy. The default raw converter simply performs a <code class="computeroutput"><span class="keyword">static_cast</span><span class="special">&lt;</span><span class="identifier">Target</span><span class="special">&gt;(</span><span class="identifier">source</span><span class="special">)</span></code>.
</p>
<p>
However, if the a UDT is involved, the <code class="computeroutput"><span class="keyword">static_cast</span></code>
might not work. In this case, the user can implement and pass a different
raw converter policy. See <a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_rawconverter" title="Policy RawConverter">RawConverter</a>
policy for details
</p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2004 -2007 Fernando Luis Cacciola Carballal<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="converter___function_object.html"><img src="../../../../../../doc/html/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../../doc/html/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../../doc/html/images/home.png" alt="Home"></a><a accesskey="n" href="bounds___traits_class.html"><img src="../../../../../../doc/html/images/next.png" alt="Next"></a>
</div>
</body>
</html>