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 <!-- Copyright Aleksey Gurtovoy 2006. Distributed under the Boost -->
5 <!-- Software License, Version 1.0. (See accompanying -->
6 <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
8 <meta http-equiv=
"Content-Type" content=
"text/html; charset=utf-8" />
9 <meta name=
"generator" content=
"Docutils 0.3.6: http://docutils.sourceforge.net/" />
10 <title>THE BOOST MPL LIBRARY: Representing Dimensions
</title>
11 <link rel=
"stylesheet" href=
"../style.css" type=
"text/css" />
13 <body class=
"docframe">
14 <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> <a href=
"./representing-quantities.html" class=
"navigation-link">Next
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group">Back
<a href=
"./representing-quantities.html" class=
"navigation-link">Along
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./dimensional-analysis.html" class=
"navigation-link">Up
</a> <a href=
"../index.html" class=
"navigation-link">Home
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./tutorial_toc.html" class=
"navigation-link">Full TOC
</a></span></td>
15 <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>
16 </tr></table><div class=
"header-separator"></div>
17 <div class=
"section" id=
"representing-dimensions">
18 <h1><a class=
"toc-backref" href=
"./dimensional-analysis.html#id42" name=
"representing-dimensions">Representing Dimensions
</a></h1>
19 <p>An international standard called
<em>Système
20 International d'Unites
</em> (SI), breaks every quantity down into a
21 combination of the dimensions
<em>mass
</em>,
<em>length
</em> (or
<em>position
</em>),
22 <em>time
</em>,
<em>charge
</em>,
<em>temperature
</em>,
<em>intensity
</em>, and
<em>angle
</em>. To be
23 reasonably general, our system would have to be able to
24 represent seven or more fundamental dimensions. It also needs
25 the ability to represent composite dimensions that, like
<em>force
</em>,
26 are built through multiplication or division of the fundamental
28 <p>In general, a composite dimension is the product of powers of
29 fundamental dimensions.
<a class=
"footnote-reference" href=
"#divisor" id=
"id6" name=
"id6">[
1]
</a> If we were going to represent
30 these powers for manipulation at runtime, we could use an array of
31 seven
<tt class=
"literal"><span class=
"pre">int
</span></tt>s, with each position in the array holding the power
32 of a different fundamental dimension:
</p>
33 <pre class=
"literal-block">
34 typedef int dimension[
7]; // m l t ...
35 dimension const mass = {
1,
0,
0,
0,
0,
0,
0};
36 dimension const length = {
0,
1,
0,
0,
0,
0,
0};
37 dimension const time = {
0,
0,
1,
0,
0,
0,
0};
40 <table class=
"footnote" frame=
"void" id=
"divisor" rules=
"none">
41 <colgroup><col class=
"label" /><col /></colgroup>
43 <tr><td class=
"label"><a class=
"fn-backref" href=
"#id6" name=
"divisor">[
1]
</a></td><td>Divisors just contribute negative exponents, since
44 1/
<em>x
</em> =
<em>x
</em><sup>-
1</sup>.
</td></tr>
47 <p>In that representation, force would be:
</p>
48 <pre class=
"literal-block">
49 dimension const force = {
1,
1, -
2,
0,
0,
0,
0};
52 <!-- @litre_translator.line_offset -= 7 -->
53 <p>that is,
<em>mlt
</em><sup>-
2</sup>. However, if we want to get dimensions into the
54 type system, these arrays won't do the trick: they're all
55 the same type! Instead, we need types that
<em>themselves
</em> represent
56 sequences of numbers, so that two masses have the same type and a
57 mass is a different type from a length.
</p>
58 <p>Fortunately, the MPL provides us with a collection of
<strong>type
59 sequences
</strong>. For example, we can build a sequence of the built-in
60 signed integral types this way:
</p>
61 <pre class=
"literal-block">
62 #include
<boost/mpl/vector.hpp
>
64 typedef boost::mpl::vector
<
65 signed char, short, int, long
> signed_types;
67 <p>How can we use a type sequence to represent numbers? Just as
68 numerical metafunctions pass and return wrapper
<em>types
</em> having a
69 nested
<tt class=
"literal"><span class=
"pre">::value
</span></tt>, so numerical sequences are really sequences of
70 wrapper types (another example of polymorphism). To make this sort
71 of thing easier, MPL supplies the
<tt class=
"literal"><span class=
"pre">int_
<N
></span></tt> class template, which
72 presents its integral argument as a nested
<tt class=
"literal"><span class=
"pre">::value
</span></tt>:
</p>
73 <pre class=
"literal-block">
74 #include
<boost/mpl/int.hpp
>
76 namespace mpl = boost::mpl; // namespace alias
77 static int const five = mpl::int_
<5>::value;
80 <p class=
"sidebar-title first">Namespace Aliases
</p>
81 <div class=
"line-block">
82 <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>
84 <p>declares
<em>alias
</em> to be a synonym for
<em>namespace-name
</em>. Many
85 examples in this book will use
<tt class=
"literal"><span class=
"pre">mpl::
</span></tt> to indicate
86 <tt class=
"literal"><span class=
"pre">boost::mpl::
</span></tt>, but will omit the alias that makes it legal
89 <!-- @ignore() # nonsense isn't worth testing
91 #include <boost/mpl/int.hpp>
92 #include <boost/mpl/vector.hpp>
94 <p>In fact, the library contains a whole suite of integral constant
95 wrappers 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
96 different type of integral constant within a class template.
</p>
97 <p>Now we can build our fundamental dimensions:
</p>
98 <pre class=
"literal-block">
99 typedef mpl::vector
<
100 mpl::int_
<1>, mpl::int_
<0>, mpl::int_
<0>, mpl::int_
<0>
101 , mpl::int_
<0>, mpl::int_
<0>, mpl::int_
<0>
104 typedef mpl::vector
<
105 mpl::int_
<0>, mpl::int_
<1>, mpl::int_
<0>, mpl::int_
<0>
106 , mpl::int_
<0>, mpl::int_
<0>, mpl::int_
<0>
110 <!-- @ # We explained about the implicit namespace alias above
112 namespace boost{namespace mpl {}}
113 namespace mpl = boost::mpl;
116 <p>Whew! That's going to get tiring pretty quickly. Worse, it's hard
117 to read and verify: The essential information, the powers of each
118 fundamental dimension, is buried in repetitive syntactic
"noise.
"
119 Accordingly, MPL supplies
<strong>integral sequence wrappers
</strong> that allow
121 <pre class=
"literal-block">
122 #include
<boost/mpl/vector_c.hpp
>
124 typedef mpl::vector_c
<int,
1,
0,
0,
0,
0,
0,
0> mass;
125 typedef mpl::vector_c
<int,
0,
1,
0,
0,
0,
0,
0> length; // or position
126 typedef mpl::vector_c
<int,
0,
0,
1,
0,
0,
0,
0> time;
127 typedef mpl::vector_c
<int,
0,
0,
0,
1,
0,
0,
0> charge;
128 typedef mpl::vector_c
<int,
0,
0,
0,
0,
1,
0,
0> temperature;
129 typedef mpl::vector_c
<int,
0,
0,
0,
0,
0,
1,
0> intensity;
130 typedef mpl::vector_c
<int,
0,
0,
0,
0,
0,
0,
1> angle;
132 <p>Even though they have different types, you can think of these
133 <tt class=
"literal"><span class=
"pre">mpl::vector_c
</span></tt> specializations as being equivalent to the more
134 verbose versions above that use
<tt class=
"literal"><span class=
"pre">mpl::vector
</span></tt>.
</p>
135 <p>If we want, we can also define a few composite dimensions:
</p>
136 <pre class=
"literal-block">
137 // base dimension: m l t ...
138 typedef mpl::vector_c
<int,
0,
1,-
1,
0,
0,
0,
0> velocity; // l/t
139 typedef mpl::vector_c
<int,
0,
1,-
2,
0,
0,
0,
0> acceleration; // l/(t
<sup>2</sup>)
140 typedef mpl::vector_c
<int,
1,
1,-
1,
0,
0,
0,
0> momentum; // ml/t
141 typedef mpl::vector_c
<int,
1,
1,-
2,
0,
0,
0,
0> force; // ml/(t
<sup>2</sup>)
143 <p>And, incidentally, the dimensions of scalars (like pi) can be
145 <pre class=
"literal-block">
146 typedef mpl::vector_c
<int,
0,
0,
0,
0,
0,
0,
0> scalar;
148 <!-- @stack[0].replace('hpp>', 'hpp>\nnamespace {')
150 compile('all', pop = None) -->
153 <div class=
"footer-separator"></div>
154 <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> <a href=
"./representing-quantities.html" class=
"navigation-link">Next
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group">Back
<a href=
"./representing-quantities.html" class=
"navigation-link">Along
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./dimensional-analysis.html" class=
"navigation-link">Up
</a> <a href=
"../index.html" class=
"navigation-link">Home
</a></span><span class=
"navigation-group-separator"> |
</span><span class=
"navigation-group"><a href=
"./tutorial_toc.html" class=
"navigation-link">Full TOC
</a></span></td>