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) --> 7<head> 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" /> 12</head> 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 20International d'Unites</em> (SI), breaks every quantity down into a 21combination 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 23reasonably general, our system would have to be able to 24represent seven or more fundamental dimensions. It also needs 25the ability to represent composite dimensions that, like <em>force</em>, 26are built through multiplication or division of the fundamental 27ones.</p> 28<p>In general, a composite dimension is the product of powers of 29fundamental dimensions. <a class="footnote-reference" href="#divisor" id="id6" name="id6">[1]</a> If we were going to represent 30these powers for manipulation at runtime, we could use an array of 31seven <tt class="literal"><span class="pre">int</span></tt>s, with each position in the array holding the power 32of a different fundamental dimension:</p> 33<pre class="literal-block"> 34typedef int dimension[7]; // m l t ... 35dimension const mass = {1, 0, 0, 0, 0, 0, 0}; 36dimension const length = {0, 1, 0, 0, 0, 0, 0}; 37dimension const time = {0, 0, 1, 0, 0, 0, 0}; 38... 39</pre> 40<table class="footnote" frame="void" id="divisor" rules="none"> 41<colgroup><col class="label" /><col /></colgroup> 42<tbody valign="top"> 43<tr><td class="label"><a class="fn-backref" href="#id6" name="divisor">[1]</a></td><td>Divisors just contribute negative exponents, since 441/<em>x</em> = <em>x</em><sup>-1</sup>.</td></tr> 45</tbody> 46</table> 47<p>In that representation, force would be:</p> 48<pre class="literal-block"> 49dimension const force = {1, 1, -2, 0, 0, 0, 0}; 50</pre> 51<!-- @compile(2) --> 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 54type system, these arrays won't do the trick: they're all 55the same type! Instead, we need types that <em>themselves</em> represent 56sequences of numbers, so that two masses have the same type and a 57mass is a different type from a length.</p> 58<p>Fortunately, the MPL provides us with a collection of <strong>type 59sequences</strong>. For example, we can build a sequence of the built-in 60signed integral types this way:</p> 61<pre class="literal-block"> 62#include <boost/mpl/vector.hpp> 63 64typedef boost::mpl::vector< 65 signed char, short, int, long> signed_types; 66</pre> 67<p>How can we use a type sequence to represent numbers? Just as 68numerical metafunctions pass and return wrapper <em>types</em> having a 69nested <tt class="literal"><span class="pre">::value</span></tt>, so numerical sequences are really sequences of 70wrapper types (another example of polymorphism). To make this sort 71of thing easier, MPL supplies the <tt class="literal"><span class="pre">int_<N></span></tt> class template, which 72presents 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> 75 76namespace mpl = boost::mpl; // namespace alias 77static int const five = mpl::int_<5>::value; 78</pre> 79<div class="sidebar"> 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> 83</div> 84<p>declares <em>alias</em> to be a synonym for <em>namespace-name</em>. Many 85examples 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 87C++.</p> 88</div> 89<!-- @ignore() # nonsense isn't worth testing 90prefix +=[''' 91 #include <boost/mpl/int.hpp> 92 #include <boost/mpl/vector.hpp> 93'''] --> 94<p>In fact, the library contains a whole suite of integral constant 95wrappers 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 96different type of integral constant within a class template.</p> 97<p>Now we can build our fundamental dimensions:</p> 98<pre class="literal-block"> 99typedef 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> 102> mass; 103 104typedef 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> 107> length; 108... 109</pre> 110<!-- @ # We explained about the implicit namespace alias above 111prefix.append(""" 112namespace boost{namespace mpl {}} 113namespace mpl = boost::mpl; 114""") 115compile('all') --> 116<p>Whew! That's going to get tiring pretty quickly. Worse, it's hard 117to read and verify: The essential information, the powers of each 118fundamental dimension, is buried in repetitive syntactic "noise." 119Accordingly, MPL supplies <strong>integral sequence wrappers</strong> that allow 120us to write:</p> 121<pre class="literal-block"> 122#include <boost/mpl/vector_c.hpp> 123 124typedef mpl::vector_c<int,1,0,0,0,0,0,0> mass; 125typedef mpl::vector_c<int,0,1,0,0,0,0,0> length; // or position 126typedef mpl::vector_c<int,0,0,1,0,0,0,0> time; 127typedef mpl::vector_c<int,0,0,0,1,0,0,0> charge; 128typedef mpl::vector_c<int,0,0,0,0,1,0,0> temperature; 129typedef mpl::vector_c<int,0,0,0,0,0,1,0> intensity; 130typedef mpl::vector_c<int,0,0,0,0,0,0,1> angle; 131</pre> 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 134verbose 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 ... 138typedef mpl::vector_c<int,0,1,-1,0,0,0,0> velocity; // l/t 139typedef mpl::vector_c<int,0,1,-2,0,0,0,0> acceleration; // l/(t<sup>2</sup>) 140typedef mpl::vector_c<int,1,1,-1,0,0,0,0> momentum; // ml/t 141typedef mpl::vector_c<int,1,1,-2,0,0,0,0> force; // ml/(t<sup>2</sup>) 142</pre> 143<p>And, incidentally, the dimensions of scalars (like pi) can be 144described as:</p> 145<pre class="literal-block"> 146typedef mpl::vector_c<int,0,0,0,0,0,0,0> scalar; 147</pre> 148<!-- @stack[0].replace('hpp>', 'hpp>\nnamespace {') 149stack[0].append('}') 150compile('all', pop = None) --> 151</div> 152 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> 155</tr></table></body> 156</html> 157