1<?xml version="1.0" encoding="utf-8" ?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 4<head> 5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 6<meta name="generator" content="Docutils 0.7: http://docutils.sourceforge.net/" /> 7<title>The MPL Reference Manual: numeric_cast</title> 8<link rel="stylesheet" href="../style.css" type="text/css" /> 9</head> 10<body class="docframe refmanual"> 11<table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./inherit-linearly.html" class="navigation-link">Prev</a> <a href="./min.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./inherit-linearly.html" class="navigation-link">Back</a> <a href="./min.html" class="navigation-link">Along</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./miscellaneous.html" class="navigation-link">Up</a> <a href="../refmanual.html" class="navigation-link">Home</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./refmanual_toc.html" class="navigation-link">Full TOC</a></span></td> 12<td class="header-group page-location"><a href="../refmanual.html" class="navigation-link">Front Page</a> / <a href="./metafunctions.html" class="navigation-link">Metafunctions</a> / <a href="./miscellaneous.html" class="navigation-link">Miscellaneous</a> / <a href="./numeric-cast.html" class="navigation-link">numeric_cast</a></td> 13</tr></table><div class="header-separator"></div> 14<div class="section" id="numeric-cast"> 15<h1><a class="toc-backref" href="./miscellaneous.html#id1568">numeric_cast</a></h1> 16<div class="section" id="id1181"> 17<h3><a class="subsection-title" href="#synopsis" name="synopsis">Synopsis</a></h3> 18<pre class="literal-block"> 19template< 20 typename SourceTag 21 , typename TargetTag 22 > 23struct <a href="./numeric-cast.html" class="identifier">numeric_cast</a>; 24</pre> 25</div> 26<div class="section" id="id1182"> 27<h3><a class="subsection-title" href="#description" name="description">Description</a></h3> 28<p>Each <tt class="literal"><span class="pre"><a href="./numeric-cast.html" class="identifier">numeric_cast</a></span></tt> specialization is a user-specialized unary <a class="reference internal" href="./metafunction-class.html">Metafunction Class</a> 29providing a conversion between two numeric types.</p> 30</div> 31<div class="section" id="id1183"> 32<h3><a class="subsection-title" href="#header" name="header">Header</a></h3> 33<pre class="literal-block"> 34#include <<a href="../../../../boost/mpl/numeric_cast.hpp" class="header">boost/mpl/numeric_cast.hpp</a>> 35</pre> 36</div> 37<div class="section" id="id1184"> 38<h3><a class="subsection-title" href="#parameters" name="parameters">Parameters</a></h3> 39<table border="1" class="docutils table"> 40<colgroup> 41<col width="17%" /> 42<col width="30%" /> 43<col width="53%" /> 44</colgroup> 45<thead valign="bottom"> 46<tr><th class="head">Parameter</th> 47<th class="head">Requirement</th> 48<th class="head">Description</th> 49</tr> 50</thead> 51<tbody valign="top"> 52<tr><td><tt class="literal"><span class="pre">SourceTag</span></tt></td> 53<td><a class="reference internal" href="./integral-constant.html">Integral Constant</a></td> 54<td>A tag for the conversion's source type.</td> 55</tr> 56<tr><td><tt class="literal"><span class="pre">TargetTag</span></tt></td> 57<td><a class="reference internal" href="./integral-constant.html">Integral Constant</a></td> 58<td>A tag for the conversion's destination type.</td> 59</tr> 60</tbody> 61</table> 62</div> 63<div class="section" id="id1185"> 64<h3><a class="subsection-title" href="#expression-semantics" name="expression-semantics">Expression semantics</a></h3> 65<p>If <tt class="literal"><span class="pre">x</span></tt> and <tt class="literal"><span class="pre">y</span></tt> are two numeric types, <tt class="literal"><span class="pre">x</span></tt> is convertible to <tt class="literal"><span class="pre">y</span></tt>, and 66<tt class="literal"><span class="pre">x_tag</span></tt> and <tt class="literal"><span class="pre">y_tag</span></tt> are the types' corresponding <a class="reference internal" href="./integral-constant.html">Integral Constant</a> tags:</p> 67<pre class="literal-block"> 68typedef <a href="./apply-wrap.html" class="identifier">apply_wrap</a><tt class="literal"><span class="pre">2</span></tt>< <a href="./numeric-cast.html" class="identifier">numeric_cast</a><x_tag,y_tag>,x >::type r; 69</pre> 70<table class="docutils field-list" frame="void" rules="none"> 71<col class="field-name" /> 72<col class="field-body" /> 73<tbody valign="top"> 74<tr class="field"><th class="field-name">Return type:</th><td class="field-body">A type.</td> 75</tr> 76<tr class="field"><th class="field-name">Semantics:</th><td class="field-body"><tt class="literal"><span class="pre">r</span></tt> is a value of <tt class="literal"><span class="pre">x</span></tt> converted to the type of <tt class="literal"><span class="pre">y</span></tt>.</td> 77</tr> 78</tbody> 79</table> 80</div> 81<div class="section" id="id1186"> 82<h3><a class="subsection-title" href="#complexity" name="complexity">Complexity</a></h3> 83<p>Unspecified.</p> 84</div> 85<div class="section" id="id1187"> 86<h3><a class="subsection-title" href="#example" name="example">Example</a></h3> 87<pre class="literal-block"> 88struct complex_tag : <a href="./int.html" class="identifier">int_</a><10> {}; 89 90template< typename Re, typename Im > struct complex 91{ 92 typedef complex_tag tag; 93 typedef complex type; 94 typedef Re real; 95 typedef Im imag; 96}; 97 98template< typename C > struct real : C::real {}; 99template< typename C > struct imag : C::imag {}; 100 101namespace boost { namespace mpl { 102 103template<> struct <a href="./numeric-cast.html" class="identifier">numeric_cast</a>< integral_c_tag,complex_tag > 104{ 105 template< typename N > struct <a href="./apply.html" class="identifier">apply</a> 106 : complex< N, <a href="./integral-c.html" class="identifier">integral_c</a>< typename N::<a href="./value-type.html" class="identifier">value_type</a>, 0 > > 107 { 108 }; 109}; 110 111template<> 112struct plus_impl< complex_tag,complex_tag > 113{ 114 template< typename N1, typename N2 > struct <a href="./apply.html" class="identifier">apply</a> 115 : complex< 116 <a href="./plus.html" class="identifier">plus</a>< typename N1::real, typename N2::real > 117 , <a href="./plus.html" class="identifier">plus</a>< typename N1::imag, typename N2::imag > 118 > 119 { 120 }; 121}; 122 123}} 124 125typedef <a href="./int.html" class="identifier">int_</a><2> i; 126typedef complex< <a href="./int.html" class="identifier">int_</a><5>, <a href="./int.html" class="identifier">int_</a><-1> > c1; 127typedef complex< <a href="./int.html" class="identifier">int_</a><-5>, <a href="./int.html" class="identifier">int_</a><1> > c2; 128 129typedef <a href="./plus.html" class="identifier">plus</a><c1,i> r4; 130<a href="./assert-relation.html" class="identifier">BOOST_MPL_ASSERT_RELATION</a>( real<r4>::value, ==, 7 ); 131<a href="./assert-relation.html" class="identifier">BOOST_MPL_ASSERT_RELATION</a>( imag<r4>::value, ==, -1 ); 132 133typedef <a href="./plus.html" class="identifier">plus</a><i,c2> r5; 134<a href="./assert-relation.html" class="identifier">BOOST_MPL_ASSERT_RELATION</a>( real<r5>::value, ==, -3 ); 135<a href="./assert-relation.html" class="identifier">BOOST_MPL_ASSERT_RELATION</a>( imag<r5>::value, ==, 1 ); 136</pre> 137</div> 138<div class="section" id="id1188"> 139<h3><a class="subsection-title" href="#see-also" name="see-also">See also</a></h3> 140<p><a class="reference internal" href="./metafunctions.html">Metafunctions</a>, <a class="reference internal" href="./numeric-metafunction.html">Numeric Metafunction</a>, <a class="reference internal" href="./plus.html">plus</a>, <a class="reference internal" href="./minus.html">minus</a>, <a class="reference internal" href="./times.html">times</a></p> 141<!-- Metafunctions/Miscellaneous//min |80 --> 142</div> 143</div> 144 145<div class="footer-separator"></div> 146<table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./inherit-linearly.html" class="navigation-link">Prev</a> <a href="./min.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./inherit-linearly.html" class="navigation-link">Back</a> <a href="./min.html" class="navigation-link">Along</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./miscellaneous.html" class="navigation-link">Up</a> <a href="../refmanual.html" class="navigation-link">Home</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./refmanual_toc.html" class="navigation-link">Full TOC</a></span></td> 147<td><div class="copyright-footer"><div class="copyright">Copyright © 2001-2009 Aleksey Gurtovoy and David Abrahams</div> 148Distributed under the Boost Software License, Version 1.0. (See accompanying 149file LICENSE_1_0.txt or copy at <a class="reference external" href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)</div></td></tr></table></body> 150</html> 151