1.. Metafunctions/Miscellaneous//numeric_cast |50 2 3numeric_cast 4============ 5 6Synopsis 7-------- 8 9.. parsed-literal:: 10 11 template< 12 typename SourceTag 13 , typename TargetTag 14 > 15 struct numeric_cast; 16 17 18Description 19----------- 20 21Each ``numeric_cast`` specialization is a user-specialized unary |Metafunction Class| 22providing a conversion between two numeric types. 23 24 25Header 26------ 27 28.. parsed-literal:: 29 30 #include <boost/mpl/numeric_cast.hpp> 31 32 33Parameters 34---------- 35 36+---------------+---------------------------+-----------------------------------------------+ 37| Parameter | Requirement | Description | 38+===============+===========================+===============================================+ 39| ``SourceTag`` | |Integral Constant| | A tag for the conversion's source type. | 40+---------------+---------------------------+-----------------------------------------------+ 41| ``TargetTag`` | |Integral Constant| | A tag for the conversion's destination type. | 42+---------------+---------------------------+-----------------------------------------------+ 43 44 45Expression semantics 46-------------------- 47 48If ``x`` and ``y`` are two numeric types, ``x`` is convertible to ``y``, and 49``x_tag`` and ``y_tag`` are the types' corresponding |Integral Constant| tags: 50 51 52.. parsed-literal:: 53 54 typedef apply_wrap\ ``2``\< numeric_cast<x_tag,y_tag>,x >::type r; 55 56:Return type: 57 A type. 58 59:Semantics: 60 ``r`` is a value of ``x`` converted to the type of ``y``. 61 62 63Complexity 64---------- 65 66Unspecified. 67 68 69Example 70------- 71 72.. parsed-literal:: 73 74 struct complex_tag : int_<10> {}; 75 76 template< typename Re, typename Im > struct complex 77 { 78 typedef complex_tag tag; 79 typedef complex type; 80 typedef Re real; 81 typedef Im imag; 82 }; 83 84 template< typename C > struct real : C::real {}; 85 template< typename C > struct imag : C::imag {}; 86 87 namespace boost { namespace mpl { 88 89 template<> struct numeric_cast< integral_c_tag,complex_tag > 90 { 91 template< typename N > struct apply 92 : complex< N, integral_c< typename N::value_type, 0 > > 93 { 94 }; 95 }; 96 97 template<> 98 struct plus_impl< complex_tag,complex_tag > 99 { 100 template< typename N1, typename N2 > struct apply 101 : complex< 102 plus< typename N1::real, typename N2::real > 103 , plus< typename N1::imag, typename N2::imag > 104 > 105 { 106 }; 107 }; 108 109 }} 110 111 typedef int_<2> i; 112 typedef complex< int_<5>, int_<-1> > c1; 113 typedef complex< int_<-5>, int_<1> > c2; 114 115 typedef plus<c1,i> r4; 116 BOOST_MPL_ASSERT_RELATION( real<r4>::value, ==, 7 ); 117 BOOST_MPL_ASSERT_RELATION( imag<r4>::value, ==, -1 ); 118 119 typedef plus<i,c2> r5; 120 BOOST_MPL_ASSERT_RELATION( real<r5>::value, ==, -3 ); 121 BOOST_MPL_ASSERT_RELATION( imag<r5>::value, ==, 1 ); 122 123 124See also 125-------- 126 127|Metafunctions|, |Numeric Metafunction|, |plus|, |minus|, |times| 128 129 130.. copyright:: Copyright � 2001-2009 Aleksey Gurtovoy and David Abrahams 131 Distributed under the Boost Software License, Version 1.0. (See accompanying 132 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 133