Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/mpl/doc/tutorial/representing-dimensions.html @ 12

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

added boost

File size: 9.4 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: Representing Dimensions</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="./dimensional-analysis.html" class="navigation-link">Prev</a>&nbsp;<a href="./representing-quantities.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group">Back&nbsp;<a href="./representing-quantities.html" class="navigation-link">Along</a></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="./representing-dimensions.html" class="navigation-link">Representing Dimensions</a></td>
13</tr></table><div class="header-separator"></div>
14<div class="section" id="representing-dimensions">
15<h1><a class="toc-backref" href="./dimensional-analysis.html#id42" name="representing-dimensions">Representing Dimensions</a></h1>
16<p>An international standard called <em>Système
17International d'Unites</em> (SI), breaks every quantity down into a
18combination of the dimensions <em>mass</em>, <em>length</em> (or <em>position</em>),
19<em>time</em>, <em>charge</em>, <em>temperature</em>, <em>intensity</em>, and <em>angle</em>.  To be
20reasonably general, our system would have to be able to
21represent seven or more fundamental dimensions.  It also needs
22the ability to represent composite dimensions that, like <em>force</em>,
23are built through multiplication or division of the fundamental
24ones.</p>
25<p>In general, a composite dimension is the product of powers of
26fundamental dimensions. <a class="footnote-reference" href="#divisor" id="id6" name="id6">[1]</a>  If we were going to represent
27these powers for manipulation at runtime, we could use an array of
28seven <tt class="literal"><span class="pre">int</span></tt>s, with each position in the array holding the power
29of a different fundamental dimension:</p>
30<pre class="literal-block">
31typedef int dimension[7]; // m  l  t  ...
32dimension const mass      = {1, 0, 0, 0, 0, 0, 0};
33dimension const length    = {0, 1, 0, 0, 0, 0, 0};
34dimension const time      = {0, 0, 1, 0, 0, 0, 0};
35...
36</pre>
37<table class="footnote" frame="void" id="divisor" rules="none">
38<colgroup><col class="label" /><col /></colgroup>
39<tbody valign="top">
40<tr><td class="label"><a class="fn-backref" href="#id6" name="divisor">[1]</a></td><td>Divisors just contribute negative exponents, since
411/<em>x</em> = <em>x</em><sup>-1</sup>.</td></tr>
42</tbody>
43</table>
44<p>In that representation, force would be:</p>
45<pre class="literal-block">
46dimension const force  = {1, 1, -2, 0, 0, 0, 0};
47</pre>
48<!-- @compile(2) -->
49<!-- @litre_translator.line_offset -= 7 -->
50<p>that is, <em>mlt</em><sup>-2</sup>.  However, if we want to get dimensions into the
51type system, these arrays won't do the trick: they're all
52the same type!  Instead, we need types that <em>themselves</em> represent
53sequences of numbers, so that two masses have the same type and a
54mass is a different type from a length.</p>
55<p>Fortunately, the MPL provides us with a collection of <strong>type
56sequences</strong>.  For example, we can build a sequence of the built-in
57signed integral types this way:</p>
58<pre class="literal-block">
59#include &lt;boost/mpl/vector.hpp&gt;
60
61typedef boost::mpl::vector&lt;
62     signed char, short, int, long&gt; signed_types;
63</pre>
64<p>How can we use a type sequence to represent numbers?  Just as
65numerical metafunctions pass and return wrapper <em>types</em> having a
66nested <tt class="literal"><span class="pre">::value</span></tt>, so numerical sequences are really sequences of
67wrapper types (another example of polymorphism).  To make this sort
68of thing easier, MPL supplies the <tt class="literal"><span class="pre">int_&lt;N&gt;</span></tt> class template, which
69presents its integral argument as a nested <tt class="literal"><span class="pre">::value</span></tt>:</p>
70<pre class="literal-block">
71#include &lt;boost/mpl/int.hpp&gt;
72
73namespace mpl = boost::mpl; // namespace alias
74static int const five = mpl::int_&lt;5&gt;::value;
75</pre>
76<div class="sidebar">
77<p class="sidebar-title first">Namespace Aliases</p>
78<div class="line-block">
79<div class="line"><tt class="literal"><span class="pre">namespace</span></tt> <em>alias</em> <tt class="literal"><span class="pre">=</span></tt> <em>namespace-name</em><tt class="literal"><span class="pre">;</span></tt></div>
80</div>
81<p>declares <em>alias</em> to be a synonym for <em>namespace-name</em>.  Many
82examples in this book will use <tt class="literal"><span class="pre">mpl::</span></tt> to indicate
83<tt class="literal"><span class="pre">boost::mpl::</span></tt>, but will omit the alias that makes it legal
84C++.</p>
85</div>
86<!-- @ignore() # nonsense isn't worth testing
87prefix +=['''
88    #include <boost/mpl/int.hpp>
89    #include <boost/mpl/vector.hpp>
90'''] -->
91<p>In fact, the library contains a whole suite of integral constant
92wrappers such as <tt class="literal"><span class="pre">long_</span></tt> and <tt class="literal"><span class="pre">bool_</span></tt>, each one wrapping a
93different type of integral constant within a class template.</p>
94<p>Now we can build our fundamental dimensions:</p>
95<pre class="literal-block">
96typedef mpl::vector&lt;
97   mpl::int_&lt;1&gt;, mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;
98 , mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;
99&gt; mass;
100
101typedef mpl::vector&lt;
102   mpl::int_&lt;0&gt;, mpl::int_&lt;1&gt;, mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;
103 , mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;
104&gt; length;
105...
106</pre>
107<!-- @ # We explained about the implicit namespace alias above
108prefix.append("""
109namespace boost{namespace mpl {}}
110namespace mpl = boost::mpl;
111""")
112compile('all') -->
113<p>Whew!  That's going to get tiring pretty quickly.  Worse, it's hard
114to read and verify:  The essential information, the powers of each
115fundamental dimension, is buried in repetitive syntactic &quot;noise.&quot;
116Accordingly, MPL supplies <strong>integral sequence wrappers</strong> that allow
117us to write:</p>
118<pre class="literal-block">
119#include &lt;boost/mpl/vector_c.hpp&gt;
120
121typedef mpl::vector_c&lt;int,1,0,0,0,0,0,0&gt; mass;
122typedef mpl::vector_c&lt;int,0,1,0,0,0,0,0&gt; length; // or position
123typedef mpl::vector_c&lt;int,0,0,1,0,0,0,0&gt; time;
124typedef mpl::vector_c&lt;int,0,0,0,1,0,0,0&gt; charge;
125typedef mpl::vector_c&lt;int,0,0,0,0,1,0,0&gt; temperature;
126typedef mpl::vector_c&lt;int,0,0,0,0,0,1,0&gt; intensity;
127typedef mpl::vector_c&lt;int,0,0,0,0,0,0,1&gt; angle;
128</pre>
129<p>Even though they have different types, you can think of these
130<tt class="literal"><span class="pre">mpl::vector_c</span></tt> specializations as being equivalent to the more
131verbose versions above that use <tt class="literal"><span class="pre">mpl::vector</span></tt>.</p>
132<p>If we want, we can also define a few composite dimensions:</p>
133<pre class="literal-block">
134// base dimension:        m l  t ...
135typedef mpl::vector_c&lt;int,0,1,-1,0,0,0,0&gt; velocity;     // l/t
136typedef mpl::vector_c&lt;int,0,1,-2,0,0,0,0&gt; acceleration; // l/(t<sup>2</sup>)
137typedef mpl::vector_c&lt;int,1,1,-1,0,0,0,0&gt; momentum;     // ml/t
138typedef mpl::vector_c&lt;int,1,1,-2,0,0,0,0&gt; force;        // ml/(t<sup>2</sup>)
139</pre>
140<p>And, incidentally, the dimensions of scalars (like pi) can be
141described as:</p>
142<pre class="literal-block">
143typedef mpl::vector_c&lt;int,0,0,0,0,0,0,0&gt; scalar;
144</pre>
145<!-- @stack[0].replace('hpp>', 'hpp>\nnamespace {')
146stack[0].append('}')
147compile('all', pop = None) -->
148</div>
149
150<div class="footer-separator"></div>
151<table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./dimensional-analysis.html" class="navigation-link">Prev</a>&nbsp;<a href="./representing-quantities.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group">Back&nbsp;<a href="./representing-quantities.html" class="navigation-link">Along</a></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>
152</tr></table></body>
153</html>
Note: See TracBrowser for help on using the repository browser.