1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>converter<> function object</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="Chapter1.Boost.NumericConversion">
<link rel="up" href="../index.html" title="Chapter1.Boost.NumericConversion">
<link rel="prev" href="definitions.html" title="Definitions">
<link rel="next" href="type_requirements_and_user_defined_types_support.html" title="Type Requirements and User-defined-types support">
</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="definitions.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="type_requirements_and_user_defined_types_support.html"><img src="../../../../../../doc/html/images/next.png" alt="Next"></a>
</div>
<div class="section" title="converter<> function object">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_numericconversion.converter___function_object"></a><a class="link" href="converter___function_object.html" title="converter<> function object">converter<>
function object</a>
</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="converter___function_object.html#boost_numericconversion.converter___function_object.synopsis">Synopsis</a></span></dt>
<dt><span class="section"><a href="converter___function_object.html#boost_numericconversion.converter___function_object.template_parameters">Template
parameters</a></span></dt>
<dt><span class="section"><a href="converter___function_object.html#boost_numericconversion.converter___function_object.member_functions">Member
functions</a></span></dt>
<dt><span class="section"><a href="converter___function_object.html#boost_numericconversion.converter___function_object.range_checking_logic">Range
Checking Logic</a></span></dt>
<dt><span class="section"><a href="converter___function_object.html#boost_numericconversion.converter___function_object.examples">Examples</a></span></dt>
</dl></div>
<div class="section" title="Synopsis">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_numericconversion.converter___function_object.synopsis"></a><a class="link" href="converter___function_object.html#boost_numericconversion.converter___function_object.synopsis" title="Synopsis">Synopsis</a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">numeric</span> <span class="special">{</span>
<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span>
<span class="keyword">class</span> <span class="identifier">S</span><span class="special">,</span>
<span class="keyword">class</span> <span class="identifier">Traits</span><span class="special">,</span> <span class="special">=</span> <span class="identifier">conversion_traits</span><span class="special"><</span><span class="identifier">T</span><span class="special">,</span><span class="identifier">S</span><span class="special">></span>
<span class="keyword">class</span> <span class="identifier">OverflowHandler</span> <span class="special">=</span> <span class="identifier">def_overflow_handler</span><span class="special">,</span>
<span class="keyword">class</span> <span class="identifier">Float2IntRounder</span> <span class="special">=</span> <span class="identifier">Trunc</span><span class="special"><</span> <span class="keyword">typename</span> <span class="identifier">Traits</span><span class="special">::</span><span class="identifier">source_type</span> <span class="special">>,</span>
<span class="keyword">class</span> <span class="identifier">RawConverter</span> <span class="special">=</span> <span class="identifier">raw_converter</span><span class="special"><</span><span class="identifier">Traits</span><span class="special">>,</span>
<span class="keyword">class</span> <span class="identifier">UserRangeChecker</span> <span class="special">=</span> <span class="identifier">UseInternalRangeChecker</span>
<span class="special">></span>
<span class="keyword">struct</span> <span class="identifier">converter</span>
<span class="special">{</span>
<span class="keyword">typedef</span> <span class="identifier">Traits</span> <span class="identifier">traits</span> <span class="special">;</span>
<span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">Traits</span><span class="special">::</span><span class="identifier">source_type</span> <span class="identifier">source_type</span> <span class="special">;</span>
<span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">Traits</span><span class="special">::</span><span class="identifier">argument_type</span> <span class="identifier">argument_type</span> <span class="special">;</span>
<span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">Traits</span><span class="special">::</span><span class="identifier">result_type</span> <span class="identifier">result_type</span> <span class="special">;</span>
<span class="keyword">static</span> <span class="identifier">result_type</span> <span class="identifier">convert</span> <span class="special">(</span> <span class="identifier">argument_type</span> <span class="identifier">s</span> <span class="special">)</span> <span class="special">;</span>
<span class="identifier">result_type</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">(</span> <span class="identifier">argument_type</span> <span class="identifier">s</span> <span class="special">)</span> <span class="keyword">const</span> <span class="special">;</span>
<span class="comment">// Internal member functions:
</span>
<span class="keyword">static</span> <span class="identifier">range_check_result</span> <span class="identifier">out_of_range</span> <span class="special">(</span> <span class="identifier">argument_type</span> <span class="identifier">s</span> <span class="special">)</span> <span class="special">;</span>
<span class="keyword">static</span> <span class="keyword">void</span> <span class="identifier">validate_range</span> <span class="special">(</span> <span class="identifier">argument_type</span> <span class="identifier">s</span> <span class="special">)</span> <span class="special">;</span>
<span class="keyword">static</span> <span class="identifier">result_type</span> <span class="identifier">low_level_convert</span> <span class="special">(</span> <span class="identifier">argument_type</span> <span class="identifier">s</span> <span class="special">)</span> <span class="special">;</span>
<span class="keyword">static</span> <span class="identifier">source_type</span> <span class="identifier">nearbyint</span> <span class="special">(</span> <span class="identifier">argument_type</span> <span class="identifier">s</span> <span class="special">)</span> <span class="special">;</span>
<span class="special">}</span> <span class="special">;</span>
<span class="special">}</span> <span class="special">}</span> <span class="comment">// namespace numeric, boost
</span></pre>
<p>
<code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">converter</span><span class="special"><></span></code>
is a <a href="http://www.sgi.com/tech/stl/UnaryFunction.html" target="_top">Unary Function
Object</a> encapsulating the code to perform a numeric conversion with
the direction and properties specified by the Traits template parameter.
It can optionally take some <a class="link" href="numeric_converter_policy_classes.html" title="Numeric Converter Policy Classes">policies</a>
which can be used to customize its behavior. The <code class="computeroutput"><span class="identifier">Traits</span></code>
parameter is not a policy but the parameter that defines the conversion.
</p>
</div>
<div class="section" title="Template parameters">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_numericconversion.converter___function_object.template_parameters"></a><a class="link" href="converter___function_object.html#boost_numericconversion.converter___function_object.template_parameters" title="Template parameters">Template
parameters</a>
</h3></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
<p>
</p>
</th>
<th>
<p>
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
<code class="computeroutput"><span class="identifier">T</span></code>
</p>
</td>
<td>
<p>
The <a class="link" href="definitions.html#boost_numericconversion.definitions.numeric_types" title="Numeric Types">Numeric
Type</a> which is the <span class="emphasis"><em>Target</em></span> of the conversion.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="identifier">S</span></code>
</p>
</td>
<td>
<p>
The <a class="link" href="definitions.html#boost_numericconversion.definitions.numeric_types" title="Numeric Types">Numeric
Type</a> which is the <span class="emphasis"><em>Source</em></span> of the conversion.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="identifier">Traits</span></code>
</p>
</td>
<td>
<p>
This must be a conversion traits class with the interface of <a class="link" href="conversion_traits___traits_class.html" title="conversion_traits<> traits class"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">conversion_traits</span></code></a>
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="identifier">OverflowHandler</span></code>
</p>
</td>
<td>
<p>
<span class="bold"><strong>Stateless Policy</strong></span> called to administrate
the result of the range checking.
</p>
<p>
It is a <span class="bold"><strong>Function Object</strong></span> which receives
the result of <code class="computeroutput"><span class="identifier">out_of_range</span><span class="special">()</span></code> and is called inside the <code class="computeroutput"><span class="identifier">validate_range</span><span class="special">()</span></code>
static member function exposed by the converter.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="identifier">Float2IntRounder</span></code>
</p>
</td>
<td>
<p>
<span class="bold"><strong>Stateless Policy</strong></span> which specifies
the rounding mode used for float to integral conversions.
</p>
<p>
It supplies the <code class="computeroutput"><span class="identifier">nearbyint</span><span class="special">()</span></code> static member function exposed
by the converter.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="identifier">RawConverter</span></code>
</p>
</td>
<td>
<p>
<span class="bold"><strong>Stateless Policy</strong></span> which is used to
perform the actual conversion.
</p>
<p>
It supplies the <code class="computeroutput"><span class="identifier">low_level_convert</span><span class="special">()</span></code> static member function exposed
by the converter.
</p>
</td>
</tr>
<tr>
<td>
<p>
<code class="computeroutput"><span class="identifier">UserRangeChecker</span></code>
</p>
</td>
<td>
<p>
<span class="emphasis"><em>Special and Optional</em></span> <span class="bold"><strong>Stateless
Policy</strong></span> which can be used to override the internal range
checking logic.
</p>
<p>
If given, supplies alternative code for the <code class="computeroutput"><span class="identifier">out_of_range</span><span class="special">()</span></code> and <code class="computeroutput"><span class="identifier">validate_range</span><span class="special">()</span></code> static member functions exposed
by the converter.
</p>
</td>
</tr>
</tbody>
</table></div>
</div>
<div class="section" title="Member functions">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_numericconversion.converter___function_object.member_functions"></a><a class="link" href="converter___function_object.html#boost_numericconversion.converter___function_object.member_functions" title="Member functions">Member
functions</a>
</h3></div></div></div>
<div class="blockquote"><blockquote class="blockquote">
<p>
</p>
<p>
<code class="computeroutput"><span class="keyword">static</span> <span class="identifier">result_type</span>
<span class="identifier">converter</span><span class="special"><>::</span><span class="identifier">convert</span> <span class="special">(</span>
<span class="identifier">argument_type</span> <span class="identifier">s</span>
<span class="special">)</span> <span class="special">;</span>
<span class="comment">// throw </span></code>
</p>
<p>
</p>
</blockquote></div>
<p>
This static member function converts an rvalue of type <code class="computeroutput"><span class="identifier">source_type</span></code>
to an rvalue of type <code class="computeroutput"><span class="identifier">target_type</span></code>.
</p>
<p>
If the conversion requires it, it performs a range checking before the conversion
and passes the result of the check to the overflow handler policy (the default
policy throws an exception if out-of-range is detected)
</p>
<p>
The implementation of this function is actually built from the policies and
is basically as follows:
</p>
<pre class="programlisting"><span class="identifier">result_type</span> <span class="identifier">converter</span><span class="special"><>::</span><span class="identifier">convert</span> <span class="special">(</span> <span class="identifier">argument_type</span> <span class="identifier">s</span> <span class="special">)</span>
<span class="special">{</span>
<span class="identifier">validate_range</span><span class="special">(</span><span class="identifier">s</span><span class="special">);</span> <span class="comment">// Implemented by the internal range checking logic
</span> <span class="comment">// (which also calls the OverflowHandler policy)
</span> <span class="comment">// or externally supplied by the UserRangeChecker policy.
</span>
<span class="identifier">s</span> <span class="special">=</span> <span class="identifier">nearbyint</span><span class="special">(</span><span class="identifier">s</span><span class="special">);</span> <span class="comment">// Externally supplied by the Float2IntRounder policy.
</span> <span class="comment">// NOTE: This is actually called only for float to int conversions.
</span>
<span class="keyword">return</span> <span class="identifier">low_level_convert</span><span class="special">(</span><span class="identifier">s</span><span class="special">);</span> <span class="comment">// Externally supplied by the RawConverter policy.
</span><span class="special">}</span>
</pre>
<p>
<code class="computeroutput"><span class="identifier">converter</span><span class="special"><>::</span><span class="keyword">operator</span><span class="special">()</span> <span class="keyword">const</span></code> just calls <code class="computeroutput"><span class="identifier">convert</span><span class="special">()</span></code>
</p>
<p>
<span class="inlinemediaobject"><img src="../images/space.png" alt="space"></span>
</p>
<div class="blockquote"><blockquote class="blockquote">
<p>
</p>
<p>
<code class="computeroutput"><span class="keyword">static</span> <span class="identifier">range_check_result</span>
<span class="identifier">numeric_converter</span><span class="special"><>::</span><span class="identifier">out_of_range</span> <span class="special">(</span>
<span class="identifier">argument_type</span> <span class="identifier">s</span>
<span class="special">)</span> <span class="special">;</span></code>
</p>
<p>
</p>
</blockquote></div>
<p>
This <a class="link" href="converter___function_object.html#numeric_conversion_converter_internal">internal</a>
static member function determines if the value <code class="computeroutput"><span class="identifier">s</span></code>
can be represented by the target type without overflow.
</p>
<p>
It does not determine if the conversion is <span class="emphasis"><em>exact</em></span>; that
is, it does not detect <span class="emphasis"><em>inexact</em></span> conversions, only <span class="emphasis"><em>out-of-range</em></span>
conversions (see the <a class="link" href="definitions.html#boost_numericconversion.definitions.exact__correctly_rounded_and_out_of_range_representations" title="Exact, Correctly Rounded and Out-Of-Range Representations">Definitions</a>
for further details).
</p>
<p>
The return value is of enum type <a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.enum_range_check_result" title="enum range_check_result"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">range_check_result</span></code></a>
</p>
<p>
The actual code for the range checking logic is optimized for the combined
properties of the source and target types. For example, a non-subranged conversion
(i.e: <code class="computeroutput"><span class="keyword">int</span></code>-><code class="computeroutput"><span class="keyword">float</span></code>), requires no range checking, so <code class="computeroutput"><span class="identifier">out_of_range</span><span class="special">()</span></code>
returns <code class="computeroutput"><span class="identifier">cInRange</span></code> directly.
See the following <a class="link" href="converter___function_object.html#boost_numericconversion.converter___function_object.range_checking_logic" title="Range Checking Logic">table</a>
for more details.
</p>
<p>
If the user supplied a <a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_userrangechecker" title="Policy UserRangeChecker">UserRangeChecker</a>
policy, is this policy which implements this function, so the implementation
is user defined, although it is expected to perform the same conceptual check
and return the appropriate result.
</p>
<p>
<span class="inlinemediaobject"><img src="../images/space.png" alt="space"></span>
</p>
<div class="blockquote"><blockquote class="blockquote">
<p>
</p>
<p>
<code class="computeroutput"><span class="keyword">static</span> <span class="keyword">void</span>
<span class="identifier">numeric_converter</span><span class="special"><>::</span><span class="identifier">validate_range</span> <span class="special">(</span>
<span class="identifier">argument_type</span> <span class="identifier">s</span>
<span class="special">)</span> <span class="special">;</span>
<span class="comment">// no throw </span></code>
</p>
<p>
</p>
</blockquote></div>
<p>
This <a class="link" href="converter___function_object.html#numeric_conversion_converter_internal">internal</a>
static member function calls out_of_range(s), and passes the result to the
<a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_overflowhandler" title="Policy OverflowHandler">OverflowHandler</a>
policy class.
</p>
<p>
For those Target/Source combinations which don't require range checking,
this is an empty inline function.
</p>
<p>
If the user supplied a <a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_userrangechecker" title="Policy UserRangeChecker">UserRangeChecker</a>
policy, is this policy which implements this function, so the implementation
is user defined, although it is expected to perform the same action as the
default. In particular, it is expected to pass the result of the check to
the overflow handler.
</p>
<p>
<span class="inlinemediaobject"><img src="../images/space.png" alt="space"></span>
</p>
<div class="blockquote"><blockquote class="blockquote">
<p>
</p>
<p>
<code class="computeroutput"><span class="keyword">static</span> <span class="identifier">result_type</span>
<span class="identifier">numeric_converter</span><span class="special"><>::</span><span class="identifier">low_level_convert</span> <span class="special">(</span>
<span class="identifier">argument_type</span> <span class="identifier">s</span>
<span class="special">)</span> <span class="special">;</span></code>
</p>
<p>
</p>
</blockquote></div>
<p>
This <a class="link" href="converter___function_object.html#numeric_conversion_converter_internal">internal</a>
static member function performs the actual conversion.
</p>
<p>
This function is externally supplied by the <a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_rawconverter" title="Policy RawConverter">RawConverter</a>
policy class.
</p>
<p>
<span class="inlinemediaobject"><img src="../images/space.png" alt="space"></span>
</p>
<div class="blockquote"><blockquote class="blockquote">
<p>
</p>
<p>
<code class="computeroutput"><span class="keyword">static</span> <span class="identifier">source_type</span>
<span class="identifier">converter</span><span class="special"><>::</span><span class="identifier">nearbyint</span> <span class="special">(</span>
<span class="identifier">argument_type</span> <span class="identifier">s</span>
<span class="special">)</span> <span class="special">;</span></code>
</p>
<p>
</p>
</blockquote></div>
<p>
This <a class="link" href="converter___function_object.html#numeric_conversion_converter_internal">internal</a>
static member function, which is <span class="underline">only used</span>
for <code class="computeroutput"><span class="keyword">float</span></code> to <code class="computeroutput"><span class="keyword">int</span></code>
conversions, returns an <span class="emphasis"><em>integer</em></span> value of <span class="emphasis"><em><span class="underline">floating-point type</span></em></span> according to some
rounding direction.
</p>
<p>
This function is externally supplied by the <a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_float2introunder" title="Policy Float2IntRounder">Float2IntRounder</a>
policy class which encapsulates the specific rounding mode.
</p>
<p>
<span class="inlinemediaobject"><img src="../images/space.png" alt="space"></span>
</p>
<a name="numeric_conversion_converter_internal"></a><p>
</p>
<a name="boost_numericconversion.converter___function_object.member_functions.internal_member_functions"></a><h5>
<a name="id563268"></a>
<a class="link" href="converter___function_object.html#boost_numericconversion.converter___function_object.member_functions.internal_member_functions">Internal
Member Functions</a>
</h5>
<p>
These static member functions build the actual conversion code used by <code class="computeroutput"><span class="identifier">convert</span><span class="special">()</span></code>.
The user does not have to call these if calling <code class="computeroutput"><span class="identifier">convert</span><span class="special">()</span></code>, since <code class="computeroutput"><span class="identifier">convert</span><span class="special">()</span></code> calls them infernally, but they can be
called separately for specific needs.
</p>
</div>
<div class="section" title="Range Checking Logic">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_numericconversion.converter___function_object.range_checking_logic"></a><a class="link" href="converter___function_object.html#boost_numericconversion.converter___function_object.range_checking_logic" title="Range Checking Logic">Range
Checking Logic</a>
</h3></div></div></div>
<p>
The following table summarizes the internal range checking logic performed
for each combination of the properties of Source and Target.
</p>
<p>
LowestT/HighestT denotes the highest and lowest values of the Target type,
respectively.
</p>
<p>
<code class="computeroutput"><span class="identifier">S</span><span class="special">(</span><span class="identifier">n</span><span class="special">)</span></code> is short
for <code class="computeroutput"><span class="keyword">static_cast</span><span class="special"><</span><span class="identifier">S</span><span class="special">>(</span><span class="identifier">n</span><span class="special">)</span></code> (<code class="computeroutput"><span class="identifier">S</span></code> denotes the Source type).
</p>
<p>
<code class="computeroutput"><span class="identifier">NONE</span></code> indicates that for this
case there is no range checking.
</p>
<pre class="programlisting"><code class="literal">
int_to_int |--> sig_to_sig |--> subranged |--> ( s >= S(LowestT) ) && ( s <= S(HighestT) )
| |--> not subranged |--> NONE
|
|--> unsig_to_unsig |--> subranged |--> ( s >= S(LowestT) ) && ( s <= S(HighestT) )
| |--> not subranged |--> NONE
|
|--> sig_to_unsig |--> pos subranged |--> ( s >= S(0) ) && ( s <= S(HighestT) )
| |--> not pos subranged |--> ( s >= S(0) )
|
|--> unsig_to_sig |--> subranged |--> ( s <= S(HighestT) )
| |--> not subranged |--> NONE
</code>
<code class="literal">
int_to_float |--> NONE
</code>
<code class="literal">
float_to_int |--> round_to_zero |--> ( s > S(LowestT)-S(1) ) && ( s < S(HighestT)+S(1) )
|--> round_to_even_nearest |--> ( s >= S(LowestT)-S(0.5) ) && ( s < S(HighestT)+S(0.5) )
|--> round_to_infinity |--> ( s > S(LowestT)-S(1) ) && ( s <= S(HighestT) )
|--> round_to_neg_infinity |--> ( s >= S(LowestT) ) && ( s < S(HighestT)+S(1) )
</code>
<code class="literal">
float_to_float |--> subranged |--> ( s >= S(LowestT) ) && ( s <= S(HighestT) )
|--> not subranged |--> NONE
</code>
</pre>
</div>
<div class="section" title="Examples">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_numericconversion.converter___function_object.examples"></a><a class="link" href="converter___function_object.html#boost_numericconversion.converter___function_object.examples" title="Examples">Examples</a>
</h3></div></div></div>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">cassert</span><span class="special">></span>
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">numeric</span><span class="special">/</span><span class="identifier">conversion</span><span class="special">/</span><span class="identifier">converter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> <span class="special">{</span>
<span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">converter</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">double</span><span class="special">></span> <span class="identifier">Double2Int</span> <span class="special">;</span>
<span class="keyword">int</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">Double2Int</span><span class="special">::</span><span class="identifier">convert</span><span class="special">(</span><span class="number">2.0</span><span class="special">);</span>
<span class="identifier">assert</span> <span class="special">(</span> <span class="identifier">x</span> <span class="special">==</span> <span class="number">2</span> <span class="special">);</span>
<span class="keyword">int</span> <span class="identifier">y</span> <span class="special">=</span> <span class="identifier">Double2Int</span><span class="special">()(</span><span class="number">3.14</span><span class="special">);</span> <span class="comment">// As a function object.
</span> <span class="identifier">assert</span> <span class="special">(</span> <span class="identifier">y</span> <span class="special">==</span> <span class="number">3</span> <span class="special">)</span> <span class="special">;</span> <span class="comment">// The default rounding is trunc.
</span>
<span class="keyword">try</span>
<span class="special">{</span>
<span class="keyword">double</span> <span class="identifier">m</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">bounds</span><span class="special"><</span><span class="keyword">double</span><span class="special">>::</span><span class="identifier">highest</span><span class="special">();</span>
<span class="keyword">int</span> <span class="identifier">z</span> <span class="special">=</span> <span class="identifier">Double2Int</span><span class="special">::</span><span class="identifier">convert</span><span class="special">(</span><span class="identifier">m</span><span class="special">);</span> <span class="comment">// By default throws positive_overflow()
</span> <span class="special">}</span>
<span class="keyword">catch</span> <span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">numeric</span><span class="special">::</span><span class="identifier">positive_overflow</span> <span class="keyword">const</span><span class="special">&</span> <span class="special">)</span>
<span class="special">{</span>
<span class="special">}</span>
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
</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="definitions.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="type_requirements_and_user_defined_types_support.html"><img src="../../../../../../doc/html/images/next.png" alt="Next"></a>
</div>
</body>
</html>
|