mpl/doc/tutorial/representing-quantities.html
2007-11-03 03:25:13 +00:00

59 lines
4.7 KiB
HTML

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- Copyright Aleksey Gurtovoy 2006. Distributed under the Boost -->
<!-- Software License, Version 1.0. (See accompanying -->
<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.3.6: http://docutils.sourceforge.net/" />
<title>THE BOOST MPL LIBRARY: Representing Quantities</title>
<link rel="stylesheet" href="../style.css" type="text/css" />
</head>
<body class="docframe">
<table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./representing-dimensions.html" class="navigation-link">Prev</a>&nbsp;<a href="./implementing-addition-and.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./representing-dimensions.html" class="navigation-link">Back</a>&nbsp;<a href="./implementing-addition-and.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>
<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-quantities.html" class="navigation-link">Representing Quantities</a></td>
</tr></table><div class="header-separator"></div>
<div class="section" id="representing-quantities">
<h1><a class="toc-backref" href="./dimensional-analysis.html#id43" name="representing-quantities">Representing Quantities</a></h1>
<p>The types listed above are still pure metadata; to typecheck real
computations we'll need to somehow bind them to our runtime data.
A simple numeric value wrapper, parameterized on the number type <tt class="literal"><span class="pre">T</span></tt>
and on its dimensions, fits the bill:</p>
<pre class="literal-block">
template &lt;class T, class Dimensions&gt;
struct quantity
{
explicit quantity(T x)
: m_value(x)
{}
T value() const { return m_value; }
private:
T m_value;
};
</pre>
<!-- @ quantity_declaration = len(stack) - 1 # Remember position for later -->
<p>Now we have a way to represent numbers associated with dimensions.
For instance, we can say:</p>
<pre class="literal-block">
quantity&lt;float,length&gt; l( 1.0f );
quantity&lt;float,mass&gt; m( 2.0f );
</pre>
<p>Note that <tt class="literal"><span class="pre">Dimensions</span></tt> doesn't appear anywhere in the definition
of <tt class="literal"><span class="pre">quantity</span></tt> outside the template parameter list; its <em>only</em>
role is to ensure that <tt class="literal"><span class="pre">l</span></tt> and <tt class="literal"><span class="pre">m</span></tt> have different types.
Because they do, we cannot make the mistake of assigning a length
to a mass:</p>
<pre class="literal-block">
m = l; // compile-time type error
</pre>
<!-- @ example.wrap('void f() {', '}')
compile('all', pop = 1, expect_error = True) -->
</div>
<div class="footer-separator"></div>
<table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./representing-dimensions.html" class="navigation-link">Prev</a>&nbsp;<a href="./implementing-addition-and.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./representing-dimensions.html" class="navigation-link">Back</a>&nbsp;<a href="./implementing-addition-and.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>
</tr></table></body>
</html>