1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Numeric Converter Policy Classes</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="conversion_traits___traits_class.html" title="conversion_traits<> traits class"> 10<link rel="next" href="improved_numeric_cast__.html" title="Improved numeric_cast<>"> 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="conversion_traits___traits_class.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="improved_numeric_cast__.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.numeric_converter_policy_classes"></a><a class="link" href="numeric_converter_policy_classes.html" title="Numeric Converter Policy Classes">Numeric 28 Converter Policy Classes</a> 29</h2></div></div></div> 30<div class="toc"><dl class="toc"> 31<dt><span class="section"><a href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.enum_range_check_result">enum 32 range_check_result</a></span></dt> 33<dt><span class="section"><a href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_overflowhandler">Policy 34 OverflowHandler</a></span></dt> 35<dt><span class="section"><a href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_float2introunder">Policy 36 Float2IntRounder</a></span></dt> 37<dt><span class="section"><a href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_rawconverter">Policy 38 RawConverter</a></span></dt> 39<dt><span class="section"><a href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_userrangechecker">Policy 40 UserRangeChecker</a></span></dt> 41</dl></div> 42<div class="section"> 43<div class="titlepage"><div><div><h3 class="title"> 44<a name="boost_numericconversion.numeric_converter_policy_classes.enum_range_check_result"></a><a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.enum_range_check_result" title="enum range_check_result">enum 45 range_check_result</a> 46</h3></div></div></div> 47<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> 48 49 <span class="keyword">enum</span> <span class="identifier">range_check_result</span> 50 <span class="special">{</span> 51 <span class="identifier">cInRange</span> <span class="special">,</span> 52 <span class="identifier">cNegOverflow</span> <span class="special">,</span> 53 <span class="identifier">cPosOverflow</span> 54 <span class="special">}</span> <span class="special">;</span> 55 56<span class="special">}</span> <span class="special">}</span> 57</pre> 58<p> 59 Defines the values returned by <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><span class="identifier">out_of_range</span><span class="special">()</span></code> 60 </p> 61</div> 62<div class="section"> 63<div class="titlepage"><div><div><h3 class="title"> 64<a name="boost_numericconversion.numeric_converter_policy_classes.policy_overflowhandler"></a><a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_overflowhandler" title="Policy OverflowHandler">Policy 65 OverflowHandler</a> 66</h3></div></div></div> 67<p> 68 This <span class="emphasis"><em>stateless</em></span> non-template policy class must be a 69 <span class="emphasis"><em>function object</em></span> and is called to administrate the result 70 of the range checking. It can throw an exception if overflow has been detected 71 by the range checking as indicated by its argument. If it throws, is is recommended 72 that it be <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">bad_cast</span></code> or derived. 73 </p> 74<p> 75 It must have the following interface (it does not has to be a template class): 76 </p> 77<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">YourOverflowHandlerPolicy</span> 78<span class="special">{</span> 79 <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">(</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_check_result</span> <span class="special">)</span> <span class="special">;</span> <span class="comment">// throw bad_cast or derived</span> 80<span class="special">}</span> <span class="special">;</span> 81</pre> 82<p> 83 It is called with the result of the converter's <code class="computeroutput"><span class="identifier">out_of_range</span><span class="special">()</span></code> inside <code class="computeroutput"><span class="identifier">validate_range</span><span class="special">()</span></code>. 84 </p> 85<p> 86 These are the two overflow handler classes provided by the library: 87 </p> 88<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> 89 90 <span class="keyword">struct</span> <span class="identifier">def_overflow_handler</span> 91 <span class="special">{</span> 92 <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">(</span> <span class="identifier">range_check_result</span> <span class="identifier">r</span> <span class="special">)</span> <span class="comment">// throw bad_numeric_conversion derived</span> 93 <span class="special">{</span> 94 <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">r</span> <span class="special">==</span> <span class="identifier">cNegOverflow</span> <span class="special">)</span> 95 <span class="keyword">throw</span> <span class="identifier">negative_overflow</span><span class="special">()</span> <span class="special">;</span> 96 <span class="keyword">else</span> <span class="keyword">if</span> <span class="special">(</span> <span class="identifier">r</span> <span class="special">==</span> <span class="identifier">cPosOverflow</span> <span class="special">)</span> 97 <span class="keyword">throw</span> <span class="identifier">positive_overflow</span><span class="special">()</span> <span class="special">;</span> 98 <span class="special">}</span> 99 <span class="special">}</span> <span class="special">;</span> 100 101 <span class="keyword">struct</span> <span class="identifier">silent_overflow_handler</span> 102 <span class="special">{</span> 103 <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">(</span> <span class="identifier">range_check_result</span> <span class="special">)</span> <span class="comment">// no-throw</span> 104 <span class="special">{}</span> 105 <span class="special">}</span> <span class="special">;</span> 106 107<span class="special">}</span> <span class="special">}</span> 108</pre> 109<p> 110 And these are the Exception Classes thrown by the default overflow handler 111 <a class="link" href="numeric_converter_policy_classes.html#numeric_conversion_policy_overflow_handler_important_note">(see 112 IMPORTANT note)</a> 113 </p> 114<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> 115 116 <a name="numeric_conversion_bad_numeric_cast"></a> 117 <span class="keyword">class</span> <span class="identifier">bad_numeric_cast</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">bad_cast</span> 118 <span class="special">{</span> 119 <span class="keyword">public</span><span class="special">:</span> 120 <span class="keyword">virtual</span> <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">what</span><span class="special">()</span> <span class="keyword">const</span> <span class="comment">// throw()</span> 121 <span class="special">{</span> 122 <span class="keyword">return</span> <span class="string">"bad numeric conversion: overflow"</span><span class="special">;</span> 123 <span class="special">}</span> 124 125 <span class="special">};</span> 126 127 <a name="numeric_conversion_negative_overflow"></a> 128 <span class="keyword">class</span> <span class="identifier">negative_overflow</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">bad_numeric_cast</span> 129 <span class="special">{</span> 130 <span class="keyword">public</span><span class="special">:</span> 131 <span class="keyword">virtual</span> <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">what</span><span class="special">()</span> <span class="keyword">const</span> <span class="comment">// throw()</span> 132 <span class="special">{</span> 133 <span class="keyword">return</span> <span class="string">"bad numeric conversion: negative overflow"</span><span class="special">;</span> 134 <span class="special">}</span> 135 <span class="special">};</span> 136 137 <a name="numeric_conversion_possitive_overflow"></a> 138 <span class="keyword">class</span> <span class="identifier">positive_overflow</span> <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">bad_numeric_cast</span> 139 <span class="special">{</span> 140 <span class="keyword">public</span><span class="special">:</span> 141 <span class="keyword">virtual</span> <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span><span class="identifier">what</span><span class="special">()</span> <span class="keyword">const</span> <span class="comment">// throw()</span> 142 <span class="special">{</span> 143 <span class="keyword">return</span> <span class="string">"bad numeric conversion: positive overflow"</span><span class="special">;</span> 144 <span class="special">}</span> 145 <span class="special">};</span> 146 147 148<span class="special">}</span> <span class="special">}</span> 149</pre> 150<a name="numeric_conversion_policy_overflow_handler_important_note"></a><div class="important"><table border="0" summary="Important"> 151<tr> 152<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../../doc/src/images/important.png"></td> 153<th align="left">Important</th> 154</tr> 155<tr><td align="left" valign="top"><p> 156 <span class="bold"><strong>RELEASE NOTE for 1.33</strong></span> Previous to boost 157 version 1.33, the exception class <code class="computeroutput"><span class="identifier">bad_numeric_cast</span></code> 158 was named <code class="computeroutput"><span class="identifier">bad_numeric_conversion</span></code>. 159 However, in 1.33, the old function <code class="computeroutput"><span class="identifier">numeric_cast</span><span class="special"><></span></code> from <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">cast</span><span class="special">.</span><span class="identifier">hpp</span></code> 160 was completly replaced by the new <code class="computeroutput"><span class="identifier">numeric_cast</span><span class="special"><></span></code> in <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</span><span class="special">/</span><span class="identifier">cast</span><span class="special">.</span><span class="identifier">hpp</span></code> 161 (and <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">cast</span><span class="special">.</span><span class="identifier">hpp</span></code> is including <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</span><span class="special">/</span><span class="identifier">cast</span><span class="special">.</span><span class="identifier">hpp</span></code> 162 now). That old function which existed in boost for quite some time used 163 the <code class="computeroutput"><span class="identifier">bad_numeric_cast</span></code> as 164 its exception type so I decided to avoid backward compatibility problems 165 by adopting it (guessing that the user base for the old code is wider than 166 for the new code). 167 </p></td></tr> 168</table></div> 169</div> 170<div class="section"> 171<div class="titlepage"><div><div><h3 class="title"> 172<a name="boost_numericconversion.numeric_converter_policy_classes.policy_float2introunder"></a><a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_float2introunder" title="Policy Float2IntRounder">Policy 173 Float2IntRounder</a> 174</h3></div></div></div> 175<p> 176 This <span class="emphasis"><em>stateless</em></span> template policy class specifies the rounding 177 mode used for <span class="underline">float to integral</span> conversions. 178 It supplies the <code class="computeroutput"><span class="identifier">nearbyint</span><span class="special">()</span></code> static member function exposed by the converter, 179 which means that it <span class="underline">publicly inherits from this 180 policy</span>. 181 </p> 182<p> 183 The policy must have the following interface: 184 </p> 185<pre class="programlisting"><span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">></span> 186<span class="keyword">struct</span> <span class="identifier">YourFloat2IntRounderPolicy</span> 187<span class="special">{</span> 188 <span class="keyword">typedef</span> <span class="identifier">S</span> <span class="identifier">source_type</span> <span class="special">;</span> 189 <span class="keyword">typedef</span> <span class="special">{</span><span class="identifier">S</span> <span class="keyword">or</span> <span class="identifier">S</span> <span class="keyword">const</span><span class="special">&}</span> <span class="identifier">argument_type</span> <span class="special">;</span> 190 191 <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> 192 193 <span class="keyword">typedef</span> <span class="identifier">mpl</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">float_round_style</span><span class="special">,</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">round_</span><span class="special">...></span> <span class="identifier">round_style</span> <span class="special">;</span> 194 195<span class="special">}</span> <span class="special">;</span> 196</pre> 197<p> 198 These are the rounder classes provided by the library (only the specific 199 parts are shown, see the general policy form above) 200 </p> 201<div class="note"><table border="0" summary="Note"> 202<tr> 203<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td> 204<th align="left">Note</th> 205</tr> 206<tr><td align="left" valign="top"><p> 207 These classes are not intended to be general purpose rounding functions 208 but specific policies for <code class="computeroutput"><span class="identifier">converter</span><span class="special"><></span></code>. This is why they are not function 209 objects. 210 </p></td></tr> 211</table></div> 212<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> 213 214 215 <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">></span> 216 <span class="keyword">struct</span> <span class="identifier">Trunc</span> 217 <span class="special">{</span> 218 <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> 219 <span class="special">{</span> 220 <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">floor</span> <span class="special">;</span> 221 <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ceil</span> <span class="special">;</span> 222 223 <span class="keyword">return</span> <span class="identifier">s</span> <span class="special">>=</span> <span class="keyword">static_cast</span><span class="special"><</span><span class="identifier">S</span><span class="special">>(</span><span class="number">0</span><span class="special">)</span> <span class="special">?</span> <span class="identifier">floor</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">ceil</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span> <span class="special">;</span> 224 <span class="special">}</span> 225 226 <span class="keyword">typedef</span> <span class="identifier">mpl</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">float_round_style</span><span class="special">,</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">round_toward_zero</span><span class="special">></span> <span class="identifier">round_style</span> <span class="special">;</span> 227 <span class="special">}</span> <span class="special">;</span> 228 229 230 <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">></span> 231 <span class="keyword">struct</span> <span class="identifier">RoundEven</span> 232 <span class="special">{</span> 233 <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> 234 <span class="special">{</span> 235 <span class="keyword">return</span> <span class="identifier">impl</span><span class="special">-</span><span class="identifier">defined</span><span class="special">-</span><span class="identifier">value</span> <span class="special">;</span> 236 <span class="special">}</span> 237 238 <span class="keyword">typedef</span> <span class="identifier">mpl</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">float_round_style</span><span class="special">,</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">round_to_nearest</span><span class="special">></span> <span class="identifier">round_style</span> <span class="special">;</span> 239 <span class="special">}</span> <span class="special">;</span> 240 241 242 <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">></span> 243 <span class="keyword">struct</span> <span class="identifier">Ceil</span> 244 <span class="special">{</span> 245 <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> 246 <span class="special">{</span> 247 <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ceil</span> <span class="special">;</span> 248 <span class="keyword">return</span> <span class="identifier">ceil</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span> <span class="special">;</span> 249 <span class="special">}</span> 250 251 <span class="keyword">typedef</span> <span class="identifier">mpl</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">float_round_style</span><span class="special">,</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">round_toward_infinity</span><span class="special">></span> <span class="identifier">round_style</span> <span class="special">;</span> 252 <span class="special">}</span> <span class="special">;</span> 253 254 255 <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">S</span><span class="special">></span> 256 <span class="keyword">struct</span> <span class="identifier">Floor</span> 257 <span class="special">{</span> 258 <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> 259 <span class="special">{</span> 260 <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">floor</span> <span class="special">;</span> 261 <span class="keyword">return</span> <span class="identifier">floor</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span> <span class="special">;</span> 262 <span class="special">}</span> 263 <span class="keyword">typedef</span> <span class="identifier">mpl</span><span class="special">::</span><span class="identifier">integral_c</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">float_round_style</span><span class="special">,</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">round_toward_neg_infinity</span><span class="special">></span> <span class="identifier">round_style</span> <span class="special">;</span> 264 <span class="special">}</span> <span class="special">;</span> 265 266<span class="special">}</span> <span class="special">}</span> <span class="comment">// namespace numeric, namespace boost</span> 267</pre> 268<h5> 269<a name="boost_numericconversion.numeric_converter_policy_classes.policy_float2introunder.h0"></a> 270 <span class="phrase"><a name="boost_numericconversion.numeric_converter_policy_classes.policy_float2introunder.math_functions_used_by_the_rounder_policies"></a></span><a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_float2introunder.math_functions_used_by_the_rounder_policies">Math 271 Functions used by the rounder policies</a> 272 </h5> 273<p> 274 The rounder policies supplied by this header use math functions <code class="computeroutput"><span class="identifier">floor</span><span class="special">()</span></code> 275 and <code class="computeroutput"><span class="identifier">ceil</span><span class="special">()</span></code>. 276 The standard versions of these functions are introduced in context by a using 277 directive, so in normal conditions, the standard functions will be used. 278 </p> 279<p> 280 However, if there are other visible corresponding overloads an ambiguity 281 could arise. In this case, the user can supply her own rounder policy which 282 could, for instance, use a fully qualified call. 283 </p> 284<p> 285 This technique allows the default rounder policies to be used directly with 286 user defined types. The user only requires that suitable overloads of <code class="computeroutput"><span class="identifier">floor</span><span class="special">()</span></code> 287 and <code class="computeroutput"><span class="identifier">ceil</span><span class="special">()</span></code> 288 be visible. See also <a class="link" href="type_requirements_and_user_defined_types_support.html" title="Type Requirements and User-defined-types support">User 289 Defined Numeric Types</a> support. 290 </p> 291</div> 292<div class="section"> 293<div class="titlepage"><div><div><h3 class="title"> 294<a name="boost_numericconversion.numeric_converter_policy_classes.policy_rawconverter"></a><a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_rawconverter" title="Policy RawConverter">Policy 295 RawConverter</a> 296</h3></div></div></div> 297<p> 298 This <span class="emphasis"><em>stateless</em></span> template policy class is used to perform 299 the actual conversion from Source to Target. It supplies the <code class="computeroutput"><span class="identifier">low_level_convert</span><span class="special">()</span></code> 300 static member function exposed by the converter, which means that it publicly 301 inherits from this policy. 302 </p> 303<p> 304 The policy must have the following interface: 305 </p> 306<pre class="programlisting"><span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">Traits</span><span class="special">></span> 307<span class="keyword">struct</span> <span class="identifier">YourRawConverterPolicy</span> 308<span class="special">{</span> 309 <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> 310 <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> 311 312 <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">return</span> <span class="special"><</span><span class="identifier">impl</span> <span class="identifier">defined</span><span class="special">></span> <span class="special">;</span> <span class="special">}</span> 313<span class="special">}</span> <span class="special">;</span> 314</pre> 315<p> 316 This policy is mostly provided as a hook for user defined types which don't 317 support <code class="computeroutput"><span class="keyword">static_cast</span><span class="special"><></span></code> 318 conversions to some types 319 </p> 320<p> 321 This is the only raw converter policy class provided by the library: 322 </p> 323<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> 324 325 <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">Traits</span><span class="special">></span> 326 <span class="keyword">struct</span> <span class="identifier">raw_numeric_converter</span> 327 <span class="special">{</span> 328 <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> 329 <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> 330 331 <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> 332 <span class="special">{</span> 333 <span class="keyword">return</span> <span class="keyword">static_cast</span><span class="special"><</span><span class="identifier">result_type</span><span class="special">>(</span><span class="identifier">s</span><span class="special">)</span> <span class="special">;</span> 334 <span class="special">}</span> 335 <span class="special">}</span> <span class="special">;</span> 336 337<span class="special">}</span> <span class="special">}</span> 338</pre> 339</div> 340<div class="section"> 341<div class="titlepage"><div><div><h3 class="title"> 342<a name="boost_numericconversion.numeric_converter_policy_classes.policy_userrangechecker"></a><a class="link" href="numeric_converter_policy_classes.html#boost_numericconversion.numeric_converter_policy_classes.policy_userrangechecker" title="Policy UserRangeChecker">Policy 343 UserRangeChecker</a> 344</h3></div></div></div> 345<p> 346 This <span class="emphasis"><em>stateless</em></span> template policy class is used <span class="underline">only if supplied</span> to <span class="bold"><strong>override</strong></span> 347 the internal range checking logic. 348 </p> 349<p> 350 It supplies the <code class="computeroutput"><span class="identifier">validate_range</span><span class="special">()</span></code> static member function exposed by the converter, 351 which means that it publicly inherits from this policy. 352 </p> 353<p> 354 The policy must have the following interface: 355 </p> 356<pre class="programlisting"><span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">Traits</span><span class="special">></span> 357<span class="keyword">struct</span> <span class="identifier">YourRangeCheckerPolicy</span> 358<span class="special">{</span> 359 <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> 360 361 <span class="comment">// Determines if the value 's' fits in the range of the Target type.</span> 362 <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> 363 364 <span class="comment">// Checks whether the value 's' is out_of_range()</span> 365 <span class="comment">// and passes the result of the check to the OverflowHandler policy.</span> 366 <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> 367 <span class="special">{</span> 368 <span class="identifier">OverflowHandler</span><span class="special">()(</span> <span class="identifier">out_of_range</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span> <span class="special">)</span> <span class="special">;</span> 369 <span class="special">}</span> 370<span class="special">}</span> <span class="special">;</span> 371</pre> 372<p> 373 This policy is <span class="bold"><strong>only</strong></span> provided as a hook for 374 user defined types which require range checking (which is disabled by default 375 when a UDT is involved). The library provides a class: <code class="computeroutput"><span class="identifier">UseInternalRangeChecker</span><span class="special">{}</span></code>; which is a <span class="emphasis"><em>fake</em></span> 376 <code class="computeroutput"><span class="identifier">RangeChecker</span></code> policy used 377 to signal the converter to use its internal range checking implementation. 378 </p> 379</div> 380</div> 381<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 382<td align="left"></td> 383<td align="right"><div class="copyright-footer">Copyright © 2004-2007 Fernando 384 Luis Cacciola Carballal<p> 385 Distributed under the Boost Software License, Version 1.0. (See accompanying 386 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>) 387 </p> 388</div></td> 389</tr></table> 390<hr> 391<div class="spirit-nav"> 392<a accesskey="p" href="conversion_traits___traits_class.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="improved_numeric_cast__.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 393</div> 394</body> 395</html> 396