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