]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | .. Metafunctions/Concepts//Numeric Metafunction |60 |
2 | ||
3 | Numeric Metafunction | |
4 | ==================== | |
5 | ||
6 | Description | |
7 | ----------- | |
8 | ||
9 | A |Numeric Metafunction| is a |tag dispatched metafunction| that provides | |
10 | a built-in infrastructure for easy implementation of mixed-type operations. | |
11 | ||
12 | ||
13 | Expression requirements | |
14 | ----------------------- | |
15 | ||
16 | |In the following table...| ``op`` is a placeholder token for the actual | |
17 | |Numeric Metafunction|'s name, and ``x``, ``y`` and |x1...xn| are | |
18 | arbitrary numeric types. | |
19 | ||
20 | +-------------------------------------------+-----------------------+---------------------------+ | |
21 | | Expression | Type | Complexity | | |
22 | +===========================================+=======================+===========================+ | |
23 | |``op_tag<x>::type`` | |Integral Constant| | Amortized constant time. | | |
24 | +-------------------------------------------+-----------------------+---------------------------+ | |
25 | | .. parsed-literal:: | Any type | Unspecified. | | |
26 | | | | | | |
27 | | op_impl< | | | | |
28 | | op_tag<x>::type | | | | |
29 | | , op_tag<y>::type | | | | |
30 | | >::apply<x,y>::type | | | | |
31 | +-------------------------------------------+-----------------------+---------------------------+ | |
32 | |``op<``\ |x1...xn|\ ``>::type`` | Any type | Unspecified. | | |
33 | +-------------------------------------------+-----------------------+---------------------------+ | |
34 | ||
35 | ||
36 | Expression semantics | |
37 | -------------------- | |
38 | ||
39 | .. parsed-literal:: | |
40 | ||
41 | typedef op_tag<x>::type tag; | |
42 | ||
43 | :Semantics: | |
44 | ``tag`` is a tag type for ``x`` for ``op``. | |
45 | ``tag::value`` is ``x``\ 's *conversion rank*. | |
46 | ||
47 | ||
48 | .. .......................................................................... | |
49 | ||
50 | .. parsed-literal:: | |
51 | ||
52 | typedef op_impl< | |
53 | op_tag<x>::type | |
54 | , op_tag<y>::type | |
55 | >::apply<x,y>::type r; | |
56 | ||
57 | :Semantics: | |
58 | ``r`` is the result of ``op`` application on arguments ``x`` | |
59 | and ``y``. | |
60 | ||
61 | ||
62 | .. .......................................................................... | |
63 | ||
64 | .. parsed-literal:: | |
65 | ||
66 | typedef op<\ |x1...xn|\ >::type r; | |
67 | ||
68 | :Semantics: | |
69 | ``r`` is the result of ``op`` application on arguments |x1...xn|. | |
70 | ||
71 | ||
72 | ||
73 | ||
74 | Example | |
75 | ------- | |
76 | ||
77 | .. parsed-literal:: | |
78 | ||
79 | ||
80 | struct complex_tag : int_<10> {}; | |
81 | ||
82 | template< typename Re, typename Im > struct complex | |
83 | { | |
84 | typedef complex_tag tag; | |
85 | typedef complex type; | |
86 | typedef Re real; | |
87 | typedef Im imag; | |
88 | }; | |
89 | ||
90 | template< typename C > struct real : C::real {}; | |
91 | template< typename C > struct imag : C::imag {}; | |
92 | ||
93 | namespace boost { namespace mpl { | |
94 | ||
95 | template<> | |
96 | struct plus_impl< complex_tag,complex_tag > | |
97 | { | |
98 | template< typename N1, typename N2 > struct apply | |
99 | : complex< | |
100 | plus< typename N1::real, typename N2::real > | |
101 | , plus< typename N1::imag, typename N2::imag > | |
102 | > | |
103 | { | |
104 | }; | |
105 | }; | |
106 | ||
107 | }} | |
108 | ||
109 | typedef complex< int_<5>, int_<-1> > c1; | |
110 | typedef complex< int_<-5>, int_<1> > c2; | |
111 | ||
112 | typedef plus<c1,c2> r1; | |
113 | BOOST_MPL_ASSERT_RELATION( real<r1>::value, ==, 0 ); | |
114 | BOOST_MPL_ASSERT_RELATION( imag<r1>::value, ==, 0 ); | |
115 | ||
116 | typedef plus<c1,c1> r2; | |
117 | BOOST_MPL_ASSERT_RELATION( real<r2>::value, ==, 10 ); | |
118 | BOOST_MPL_ASSERT_RELATION( imag<r2>::value, ==, -2 ); | |
119 | ||
120 | typedef plus<c2,c2> r3; | |
121 | BOOST_MPL_ASSERT_RELATION( real<r3>::value, ==, -10 ); | |
122 | BOOST_MPL_ASSERT_RELATION( imag<r3>::value, ==, 2 ); | |
123 | ||
124 | ||
125 | ||
126 | Models | |
127 | ------ | |
128 | ||
129 | * |plus| | |
130 | * |minus| | |
131 | * |times| | |
132 | * |divides| | |
133 | ||
134 | ||
135 | See also | |
136 | -------- | |
137 | ||
138 | |Tag Dispatched Metafunction|, |Metafunctions|, |numeric_cast| | |
139 | ||
140 | ||
141 |