• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. Metafunctions/Concepts//Numeric Metafunction |60
2
3Numeric Metafunction
4====================
5
6Description
7-----------
8
9A |Numeric Metafunction| is a |tag dispatched metafunction| that provides
10a built-in infrastructure for easy implementation of mixed-type operations.
11
12
13Expression 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
18arbitrary 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
36Expression 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
74Example
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
126Models
127------
128
129* |plus|
130* |minus|
131* |times|
132* |divides|
133
134
135See also
136--------
137
138|Tag Dispatched Metafunction|, |Metafunctions|, |numeric_cast|
139
140
141.. copyright:: Copyright �  2001-2009 Aleksey Gurtovoy and David Abrahams
142   Distributed under the Boost Software License, Version 1.0. (See accompanying
143   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
144