| 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.3.6: http://docutils.sourceforge.net/" /> |
|---|
| 7 | <title>The MPL Reference Manual: Numeric Metafunction</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="./tag-dispatched.html" class="navigation-link">Prev</a> <a href="./trivial-metafunction.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./tag-dispatched.html" class="navigation-link">Back</a> <a href="./trivial-metafunction.html" class="navigation-link">Along</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./metafunctions-concepts.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="./metafunctions-concepts.html" class="navigation-link">Concepts</a> / <a href="./numeric-metafunction.html" class="navigation-link">Numeric Metafunction</a></td> |
|---|
| 13 | </tr></table><div class="header-separator"></div> |
|---|
| 14 | <div class="section" id="numeric-metafunction"> |
|---|
| 15 | <h1><a class="toc-backref" href="./metafunctions-concepts.html#id443" name="numeric-metafunction">Numeric Metafunction</a></h1> |
|---|
| 16 | <div class="section" id="numeric-description"> |
|---|
| 17 | <h3><a class="subsection-title" href="#description" name="description">Description</a></h3> |
|---|
| 18 | <p>A <a class="reference" href="./numeric-metafunction.html">Numeric Metafunction</a> is a <a class="reference" href="./tag-dispatched.html">Tag Dispatched Metafunction</a> that provides |
|---|
| 19 | a built-in infrastructure for easy implementation of mixed-type operations.</p> |
|---|
| 20 | </div> |
|---|
| 21 | <div class="section" id="numeric-expression-requirements"> |
|---|
| 22 | <h3><a class="subsection-title" href="#expression-requirements" name="expression-requirements">Expression requirements</a></h3> |
|---|
| 23 | <p>In the following table and subsequent specifications, <tt class="literal"><span class="pre">op</span></tt> is a placeholder token for the actual |
|---|
| 24 | <a class="reference" href="./numeric-metafunction.html">Numeric Metafunction</a>'s name, and <tt class="literal"><span class="pre">x</span></tt>, <tt class="literal"><span class="pre">y</span></tt> and <em>x</em><sub>1</sub>,<em>x</em><sub>2</sub>,... <em>x</em><sub>n</sub> are |
|---|
| 25 | arbitrary numeric types.</p> |
|---|
| 26 | <table border="1" class="table"> |
|---|
| 27 | <colgroup> |
|---|
| 28 | <col width="46%" /> |
|---|
| 29 | <col width="25%" /> |
|---|
| 30 | <col width="29%" /> |
|---|
| 31 | </colgroup> |
|---|
| 32 | <thead valign="bottom"> |
|---|
| 33 | <tr><th>Expression</th> |
|---|
| 34 | <th>Type</th> |
|---|
| 35 | <th>Complexity</th> |
|---|
| 36 | </tr> |
|---|
| 37 | </thead> |
|---|
| 38 | <tbody valign="top"> |
|---|
| 39 | <tr><td><tt class="literal"><span class="pre">op_tag<x>::type</span></tt></td> |
|---|
| 40 | <td><a class="reference" href="./integral-constant.html">Integral Constant</a></td> |
|---|
| 41 | <td>Amortized constant time.</td> |
|---|
| 42 | </tr> |
|---|
| 43 | <tr><td><pre class="first last literal-block"> |
|---|
| 44 | op_impl< |
|---|
| 45 | op_tag<x>::type |
|---|
| 46 | , op_tag<y>::type |
|---|
| 47 | >::<a href="./apply.html" class="identifier">apply</a><x,y>::type |
|---|
| 48 | </pre> |
|---|
| 49 | </td> |
|---|
| 50 | <td>Any type</td> |
|---|
| 51 | <td>Unspecified.</td> |
|---|
| 52 | </tr> |
|---|
| 53 | <tr><td><tt class="literal"><span class="pre">op<</span></tt><em>x</em><sub>1</sub>,<em>x</em><sub>2</sub>,... <em>x</em><sub>n</sub><tt class="literal"><span class="pre">>::type</span></tt></td> |
|---|
| 54 | <td>Any type</td> |
|---|
| 55 | <td>Unspecified.</td> |
|---|
| 56 | </tr> |
|---|
| 57 | </tbody> |
|---|
| 58 | </table> |
|---|
| 59 | </div> |
|---|
| 60 | <div class="section" id="numeric-expression-semantics"> |
|---|
| 61 | <h3><a class="subsection-title" href="#expression-semantics" name="expression-semantics">Expression semantics</a></h3> |
|---|
| 62 | <pre class="literal-block"> |
|---|
| 63 | typedef op_tag<x>::type tag; |
|---|
| 64 | </pre> |
|---|
| 65 | <table class="field-list" frame="void" rules="none"> |
|---|
| 66 | <col class="field-name" /> |
|---|
| 67 | <col class="field-body" /> |
|---|
| 68 | <tbody valign="top"> |
|---|
| 69 | <tr class="field"><th class="field-name">Semantics:</th><td class="field-body"><tt class="literal"><span class="pre">tag</span></tt> is a tag type for <tt class="literal"><span class="pre">x</span></tt> for <tt class="literal"><span class="pre">op</span></tt>. |
|---|
| 70 | <tt class="literal"><span class="pre">tag::value</span></tt> is <tt class="literal"><span class="pre">x</span></tt>'s <em>conversion rank</em>.</td> |
|---|
| 71 | </tr> |
|---|
| 72 | </tbody> |
|---|
| 73 | </table> |
|---|
| 74 | <!-- .......................................................................... --> |
|---|
| 75 | <pre class="literal-block"> |
|---|
| 76 | typedef op_impl< |
|---|
| 77 | op_tag<x>::type |
|---|
| 78 | , op_tag<y>::type |
|---|
| 79 | >::<a href="./apply.html" class="identifier">apply</a><x,y>::type r; |
|---|
| 80 | </pre> |
|---|
| 81 | <table class="field-list" frame="void" rules="none"> |
|---|
| 82 | <col class="field-name" /> |
|---|
| 83 | <col class="field-body" /> |
|---|
| 84 | <tbody valign="top"> |
|---|
| 85 | <tr class="field"><th class="field-name">Semantics:</th><td class="field-body"><tt class="literal"><span class="pre">r</span></tt> is the result of <tt class="literal"><span class="pre">op</span></tt> application on arguments <tt class="literal"><span class="pre">x</span></tt> |
|---|
| 86 | and <tt class="literal"><span class="pre">y</span></tt>.</td> |
|---|
| 87 | </tr> |
|---|
| 88 | </tbody> |
|---|
| 89 | </table> |
|---|
| 90 | <!-- .......................................................................... --> |
|---|
| 91 | <pre class="literal-block"> |
|---|
| 92 | typedef op<<em>x</em><sub>1</sub>,<em>x</em><sub>2</sub>,... <em>x</em><sub>n</sub>>::type r; |
|---|
| 93 | </pre> |
|---|
| 94 | <table class="field-list" frame="void" rules="none"> |
|---|
| 95 | <col class="field-name" /> |
|---|
| 96 | <col class="field-body" /> |
|---|
| 97 | <tbody valign="top"> |
|---|
| 98 | <tr class="field"><th class="field-name">Semantics:</th><td class="field-body"><tt class="literal"><span class="pre">r</span></tt> is the result of <tt class="literal"><span class="pre">op</span></tt> application on arguments <em>x</em><sub>1</sub>,<em>x</em><sub>2</sub>,... <em>x</em><sub>n</sub>.</td> |
|---|
| 99 | </tr> |
|---|
| 100 | </tbody> |
|---|
| 101 | </table> |
|---|
| 102 | </div> |
|---|
| 103 | <div class="section" id="numeric-example"> |
|---|
| 104 | <h3><a class="subsection-title" href="#example" name="example">Example</a></h3> |
|---|
| 105 | <pre class="literal-block"> |
|---|
| 106 | struct complex_tag : <a href="./int.html" class="identifier">int_</a><10> {}; |
|---|
| 107 | |
|---|
| 108 | template< typename Re, typename Im > struct complex |
|---|
| 109 | { |
|---|
| 110 | typedef complex_tag tag; |
|---|
| 111 | typedef complex type; |
|---|
| 112 | typedef Re real; |
|---|
| 113 | typedef Im imag; |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | template< typename C > struct real : C::real {}; |
|---|
| 117 | template< typename C > struct imag : C::imag {}; |
|---|
| 118 | |
|---|
| 119 | namespace boost { namespace mpl { |
|---|
| 120 | |
|---|
| 121 | template<> |
|---|
| 122 | struct plus_impl< complex_tag,complex_tag > |
|---|
| 123 | { |
|---|
| 124 | template< typename N1, typename N2 > struct <a href="./apply.html" class="identifier">apply</a> |
|---|
| 125 | : complex< |
|---|
| 126 | <a href="./plus.html" class="identifier">plus</a>< typename N1::real, typename N2::real > |
|---|
| 127 | , <a href="./plus.html" class="identifier">plus</a>< typename N1::imag, typename N2::imag > |
|---|
| 128 | > |
|---|
| 129 | { |
|---|
| 130 | }; |
|---|
| 131 | }; |
|---|
| 132 | |
|---|
| 133 | }} |
|---|
| 134 | |
|---|
| 135 | typedef complex< <a href="./int.html" class="identifier">int_</a><5>, <a href="./int.html" class="identifier">int_</a><-1> > c1; |
|---|
| 136 | typedef complex< <a href="./int.html" class="identifier">int_</a><-5>, <a href="./int.html" class="identifier">int_</a><1> > c2; |
|---|
| 137 | |
|---|
| 138 | typedef <a href="./plus.html" class="identifier">plus</a><c1,c2> r1; |
|---|
| 139 | <a href="./assert-relation.html" class="identifier">BOOST_MPL_ASSERT_RELATION</a>( real<r1>::value, ==, 0 ); |
|---|
| 140 | <a href="./assert-relation.html" class="identifier">BOOST_MPL_ASSERT_RELATION</a>( imag<r1>::value, ==, 0 ); |
|---|
| 141 | |
|---|
| 142 | typedef <a href="./plus.html" class="identifier">plus</a><c1,c1> r2; |
|---|
| 143 | <a href="./assert-relation.html" class="identifier">BOOST_MPL_ASSERT_RELATION</a>( real<r2>::value, ==, 10 ); |
|---|
| 144 | <a href="./assert-relation.html" class="identifier">BOOST_MPL_ASSERT_RELATION</a>( imag<r2>::value, ==, -2 ); |
|---|
| 145 | |
|---|
| 146 | typedef <a href="./plus.html" class="identifier">plus</a><c2,c2> r3; |
|---|
| 147 | <a href="./assert-relation.html" class="identifier">BOOST_MPL_ASSERT_RELATION</a>( real<r3>::value, ==, -10 ); |
|---|
| 148 | <a href="./assert-relation.html" class="identifier">BOOST_MPL_ASSERT_RELATION</a>( imag<r3>::value, ==, 2 ); |
|---|
| 149 | </pre> |
|---|
| 150 | </div> |
|---|
| 151 | <div class="section" id="numeric-models"> |
|---|
| 152 | <h3><a class="subsection-title" href="#models" name="models">Models</a></h3> |
|---|
| 153 | <ul class="simple"> |
|---|
| 154 | <li><a class="refentry reference" href="./plus.html"><tt class="refentry literal"><span class="pre">plus</span></tt></a></li> |
|---|
| 155 | <li><a class="refentry reference" href="./minus.html"><tt class="refentry literal"><span class="pre">minus</span></tt></a></li> |
|---|
| 156 | <li><a class="refentry reference" href="./times.html"><tt class="refentry literal"><span class="pre">times</span></tt></a></li> |
|---|
| 157 | <li><a class="refentry reference" href="./divides.html"><tt class="refentry literal"><span class="pre">divides</span></tt></a></li> |
|---|
| 158 | </ul> |
|---|
| 159 | </div> |
|---|
| 160 | <div class="section" id="numeric-see-also"> |
|---|
| 161 | <h3><a class="subsection-title" href="#see-also" name="see-also">See also</a></h3> |
|---|
| 162 | <p><a class="reference" href="./tag-dispatched.html">Tag Dispatched Metafunction</a>, <a class="reference" href="./metafunctions.html">Metafunctions</a>, <a class="refentry reference" href="./numeric-cast.html"><tt class="refentry literal"><span class="pre">numeric_cast</span></tt></a></p> |
|---|
| 163 | <!-- modtime: November 10, 2004 04:36:48 +0000 --> |
|---|
| 164 | <!-- Metafunctions/Concepts//Trivial Metafunction |70 --> |
|---|
| 165 | </div> |
|---|
| 166 | </div> |
|---|
| 167 | |
|---|
| 168 | <div class="footer-separator"></div> |
|---|
| 169 | <table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./tag-dispatched.html" class="navigation-link">Prev</a> <a href="./trivial-metafunction.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./tag-dispatched.html" class="navigation-link">Back</a> <a href="./trivial-metafunction.html" class="navigation-link">Along</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./metafunctions-concepts.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> |
|---|
| 170 | </tr></table></body> |
|---|
| 171 | </html> |
|---|