]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 Quantities</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="./representing-dimensions.html" class="navigation-link">Prev</a> <a href="./implementing-addition-and.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./representing-dimensions.html" class="navigation-link">Back</a> <a href="./implementing-addition-and.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-quantities.html" class="navigation-link">Representing Quantities</a></td> | |
16 | </tr></table><div class="header-separator"></div> | |
17 | <div class="section" id="representing-quantities"> | |
18 | <h1><a class="toc-backref" href="./dimensional-analysis.html#id43" name="representing-quantities">Representing Quantities</a></h1> | |
19 | <p>The types listed above are still pure metadata; to typecheck real | |
20 | computations we'll need to somehow bind them to our runtime data. | |
21 | A simple numeric value wrapper, parameterized on the number type <tt class="literal"><span class="pre">T</span></tt> | |
22 | and on its dimensions, fits the bill:</p> | |
23 | <pre class="literal-block"> | |
24 | template <class T, class Dimensions> | |
25 | struct quantity | |
26 | { | |
27 | explicit quantity(T x) | |
28 | : m_value(x) | |
29 | {} | |
30 | ||
31 | T value() const { return m_value; } | |
32 | private: | |
33 | T m_value; | |
34 | }; | |
35 | </pre> | |
36 | <!-- @ quantity_declaration = len(stack) - 1 # Remember position for later --> | |
37 | <p>Now we have a way to represent numbers associated with dimensions. | |
38 | For instance, we can say:</p> | |
39 | <pre class="literal-block"> | |
40 | quantity<float,length> l( 1.0f ); | |
41 | quantity<float,mass> m( 2.0f ); | |
42 | </pre> | |
43 | <p>Note that <tt class="literal"><span class="pre">Dimensions</span></tt> doesn't appear anywhere in the definition | |
44 | of <tt class="literal"><span class="pre">quantity</span></tt> outside the template parameter list; its <em>only</em> | |
45 | 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. | |
46 | Because they do, we cannot make the mistake of assigning a length | |
47 | to a mass:</p> | |
48 | <pre class="literal-block"> | |
49 | m = l; // compile-time type error | |
50 | </pre> | |
51 | <!-- @ example.wrap('void f() {', '}') | |
52 | compile('all', pop = 1, expect_error = True) --> | |
53 | </div> | |
54 | ||
55 | <div class="footer-separator"></div> | |
56 | <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> <a href="./implementing-addition-and.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./representing-dimensions.html" class="navigation-link">Back</a> <a href="./implementing-addition-and.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> | |
57 | </tr></table></body> | |
58 | </html> |