Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/mpl/doc/tutorial/implementing-division.html @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

File size: 9.8 KB
Line 
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 BOOST MPL LIBRARY: Implementing Division</title>
8<link rel="stylesheet" href="../style.css" type="text/css" />
9</head>
10<body class="docframe">
11<table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./implementing.html" class="navigation-link">Prev</a>&nbsp;<a href="./higher-order.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./implementing.html" class="navigation-link">Back</a>&nbsp;Along</span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./dimensional-analysis.html" class="navigation-link">Up</a>&nbsp;<a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td>
12<td class="header-group page-location"><a href="../index.html" class="navigation-link">Front Page</a> / <a href="./tutorial-metafunctions.html" class="navigation-link">Tutorial: Metafunctions and Higher-Order Metaprogramming</a> / <a href="./dimensional-analysis.html" class="navigation-link">Dimensional Analysis</a> / <a href="./implementing-division.html" class="navigation-link">Implementing Division</a></td>
13</tr></table><div class="header-separator"></div>
14<div class="section" id="implementing-division">
15<h1><a class="toc-backref" href="./dimensional-analysis.html#id46" name="implementing-division">Implementing Division</a></h1>
16<p>Division is similar to multiplication, but instead of adding
17exponents, we must subtract them.  Rather than writing out a near
18duplicate of <tt class="literal"><span class="pre">plus_f</span></tt>, we can use the following trick to make
19<tt class="literal"><span class="pre">minus_f</span></tt> much simpler:</p>
20<pre class="literal-block">
21struct minus_f
22{
23    template &lt;class T1, class T2&gt;
24    struct apply
25      : mpl::minus&lt;T1,T2&gt; {};
26};
27</pre>
28<!-- @ # The following is OK because we showed how to get at mpl_plus
29prefix.append('#include <boost/mpl/minus.hpp>')
30compile(1)  -->
31<p>Here <tt class="literal"><span class="pre">minus_f::apply</span></tt> uses inheritance to expose the nested
32<tt class="literal"><span class="pre">type</span></tt> of its base class, <tt class="literal"><span class="pre">mpl::minus</span></tt>, so we don't have to
33write:</p>
34<pre class="literal-block">
35typedef typename ...::type type
36</pre>
37<!-- @ignore() -->
38<p>We don't have to write
39<tt class="literal"><span class="pre">typename</span></tt> here (in fact, it would be illegal), because the
40compiler knows that dependent names in <tt class="literal"><span class="pre">apply</span></tt>'s initializer
41list must be base classes. <a class="footnote-reference" href="#plus-too" id="id7" name="id7">[2]</a>  This powerful
42simplification is known as <strong>metafunction forwarding</strong>; we'll apply
43it often as the book goes on. <a class="footnote-reference" href="#edg" id="id8" name="id8">[3]</a></p>
44<table class="footnote" frame="void" id="plus-too" rules="none">
45<colgroup><col class="label" /><col /></colgroup>
46<tbody valign="top">
47<tr><td class="label"><a class="fn-backref" href="#id7" name="plus-too">[2]</a></td><td>In case you're wondering, the same approach could
48have been applied to <tt class="literal"><span class="pre">plus_f</span></tt>, but since it's a little subtle,
49we introduced the straightforward but verbose formulation
50first.</td></tr>
51</tbody>
52</table>
53<table class="footnote" frame="void" id="edg" rules="none">
54<colgroup><col class="label" /><col /></colgroup>
55<tbody valign="top">
56<tr><td class="label"><a class="fn-backref" href="#id8" name="edg">[3]</a></td><td>Users of EDG-based compilers should consult <a class="reference" href="./resources.html">the book's</a> Appendix C
57for a caveat about metafunction forwarding.  You can tell whether
58you have an EDG compiler by checking the preprocessor symbol
59<tt class="literal"><span class="pre">__EDG_VERSION__</span></tt>, which is defined by all EDG-based compilers.</td></tr>
60</tbody>
61</table>
62<p>Syntactic tricks notwithstanding, writing trivial classes to wrap
63existing metafunctions is going to get boring pretty quickly.  Even
64though the definition of <tt class="literal"><span class="pre">minus_f</span></tt> was far less verbose than that
65of <tt class="literal"><span class="pre">plus_f</span></tt>, it's still an awful lot to type.  Fortunately, MPL gives
66us a <em>much</em> simpler way to pass metafunctions around.  Instead of
67building a whole metafunction class, we can invoke <tt class="literal"><span class="pre">transform</span></tt>
68this way:</p>
69<pre class="literal-block">
70typename mpl::transform&lt;D1,D2, <strong>mpl::minus&lt;_1,_2&gt;</strong> &gt;::type
71</pre>
72<!-- @# Make it harmless but legit C++ so we can syntax check later
73example.wrap('template <class D1,class D2>', 'fff(D1,D2);')
74
75# We explain placeholders below, so we can henceforth use them
76# without qualification -->
77<p>Those funny looking arguments (<tt class="literal"><span class="pre">_1</span></tt> and <tt class="literal"><span class="pre">_2</span></tt>) are known as
78<strong>placeholders</strong>, and they signify that when the <tt class="literal"><span class="pre">transform</span></tt>'s
79<tt class="literal"><span class="pre">BinaryOperation</span></tt> is invoked, its first and second arguments will
80be passed on to <tt class="literal"><span class="pre">minus</span></tt> in the positions indicated by <tt class="literal"><span class="pre">_1</span></tt> and
81<tt class="literal"><span class="pre">_2</span></tt>, respectively.  The whole type <tt class="literal"><span class="pre">mpl::minus&lt;_1,_2&gt;</span></tt> is
82known as a <strong>placeholder expression</strong>.</p>
83<div class="note">
84<p class="admonition-title first">Note</p>
85<p>MPL's placeholders are in the <tt class="literal"><span class="pre">mpl::placeholders</span></tt>
86namespace and defined in <tt class="literal"><span class="pre">boost/mpl/placeholders.hpp</span></tt>.  In
87this book we will usually assume that you have written:</p>
88<pre class="literal-block">
89#include&lt;boost/mpl/placeholders.hpp&gt;
90using namespace mpl::placeholders;
91</pre>
92<p>so that they can be accessed without qualification.</p>
93</div>
94<!-- @ prefix.append(str(example)) # move to common prefix
95ignore() -->
96<p>Here's our division operator written using placeholder
97expressions:</p>
98<pre class="literal-block">
99template &lt;class T, class D1, class D2&gt;
100quantity&lt; 
101    T
102  , typename mpl::transform&lt;D1,D2,<strong>mpl::minus&lt;_1,_2&gt;</strong> &gt;::type
103&gt;
104operator/(quantity&lt;T,D1&gt; x, quantity&lt;T,D2&gt; y)
105{
106   typedef typename
107     mpl::transform&lt;D1,D2,<strong>mpl::minus&lt;_1,_2&gt;</strong> &gt;::type dim;
108
109   return quantity&lt;T,dim&gt;( x.value() / y.value() );
110}
111</pre>
112<!-- @compile('all', pop = 1) -->
113<p>This code is considerably simpler.  We can simplify it even further
114by factoring the code that calculates the new dimensions into its
115own metafunction:</p>
116<pre class="literal-block">
117template &lt;class D1, class D2&gt;
118struct <strong>divide_dimensions</strong>
119  : mpl::transform&lt;D1,D2,mpl::minus&lt;_1,_2&gt; &gt; // forwarding again
120{};
121
122template &lt;class T, class D1, class D2&gt;
123quantity&lt;T, typename <strong>divide_dimensions&lt;D1,D2&gt;</strong>::type&gt;
124operator/(quantity&lt;T,D1&gt; x, quantity&lt;T,D2&gt; y)
125{
126   return quantity&lt;T, typename <strong>divide_dimensions&lt;D1,D2&gt;</strong>::type&gt;(
127      x.value() / y.value());
128}
129</pre>
130<!-- @compile('all', pop = None) -->
131<p>Now we can verify our &quot;force-on-a-laptop&quot; computation by reversing
132it, as follows:</p>
133<pre class="literal-block">
134quantity&lt;float,mass&gt; m2 = f/a;
135float rounding_error = std::abs((m2 - m).value());
136</pre>
137<!-- @example.wrap('''
138#include <cassert>
139#include <cmath>
140int main()
141{
142    quantity<float,mass> m(5.0f);
143    quantity<float,acceleration> a(9.8f);
144    quantity<float,force> f = m * a;
145''','''
146    assert(rounding_error < .001);
147}''')
148
149dimensional_analysis = stack[:-1] # save for later
150
151run('all') -->
152<p>If we got everything right, <tt class="literal"><span class="pre">rounding_error</span></tt> should be very close
153to zero.  These are boring calculations, but they're just the sort
154of thing that could ruin a whole program (or worse) if you got them
155wrong.  If we had written <tt class="literal"><span class="pre">a/f</span></tt> instead of <tt class="literal"><span class="pre">f/a</span></tt>, there would have
156been a compilation error, preventing a mistake from propagating
157throughout our program.</p>
158</div>
159
160<div class="footer-separator"></div>
161<table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./implementing.html" class="navigation-link">Prev</a>&nbsp;<a href="./higher-order.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./implementing.html" class="navigation-link">Back</a>&nbsp;Along</span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./dimensional-analysis.html" class="navigation-link">Up</a>&nbsp;<a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td>
162</tr></table></body>
163</html>
Note: See TracBrowser for help on using the repository browser.