1 .. Metafunctions/Concepts//Numeric Metafunction |60
9 A |Numeric Metafunction| is a |tag dispatched metafunction| that provides
10 a built-in infrastructure for easy implementation of mixed-type operations.
13 Expression requirements
14 -----------------------
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.
20 +-------------------------------------------+-----------------------+---------------------------+
21 | Expression | Type | Complexity |
22 +===========================================+=======================+===========================+
23 |``op_tag<x>::type`` | |Integral Constant| | Amortized constant time. |
24 +-------------------------------------------+-----------------------+---------------------------+
25 | .. parsed-literal:: | Any type | Unspecified. |
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 +-------------------------------------------+-----------------------+---------------------------+
41 typedef op_tag<x>::type tag;
44 ``tag`` is a tag type for ``x`` for ``op``.
45 ``tag::value`` is ``x``\ 's *conversion rank*.
48 .. ..........................................................................
55 >::apply<x,y>::type r;
58 ``r`` is the result of ``op`` application on arguments ``x``
62 .. ..........................................................................
66 typedef op<\ |x1...xn|\ >::type r;
69 ``r`` is the result of ``op`` application on arguments |x1...xn|.
80 struct complex_tag : int_<10> {};
82 template< typename Re, typename Im > struct complex
84 typedef complex_tag tag;
90 template< typename C > struct real : C::real {};
91 template< typename C > struct imag : C::imag {};
93 namespace boost { namespace mpl {
96 struct plus_impl< complex_tag,complex_tag >
98 template< typename N1, typename N2 > struct apply
100 plus< typename N1::real, typename N2::real >
101 , plus< typename N1::imag, typename N2::imag >
109 typedef complex< int_<5>, int_<-1> > c1;
110 typedef complex< int_<-5>, int_<1> > c2;
112 typedef plus<c1,c2> r1;
113 BOOST_MPL_ASSERT_RELATION( real<r1>::value, ==, 0 );
114 BOOST_MPL_ASSERT_RELATION( imag<r1>::value, ==, 0 );
116 typedef plus<c1,c1> r2;
117 BOOST_MPL_ASSERT_RELATION( real<r2>::value, ==, 10 );
118 BOOST_MPL_ASSERT_RELATION( imag<r2>::value, ==, -2 );
120 typedef plus<c2,c2> r3;
121 BOOST_MPL_ASSERT_RELATION( real<r3>::value, ==, -10 );
122 BOOST_MPL_ASSERT_RELATION( imag<r3>::value, ==, 2 );
138 |Tag Dispatched Metafunction|, |Metafunctions|, |numeric_cast|
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)