Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/mpl/doc/tutorial/exercises.html @ 12

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

added boost

File size: 7.0 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: Exercises</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="./details.html" class="navigation-link">Prev</a>&nbsp;<a href="./reference-manual.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./details.html" class="navigation-link">Back</a>&nbsp;Along</span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial-metafunctions.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="./exercises.html" class="navigation-link">Exercises</a></td>
13</tr></table><div class="header-separator"></div>
14<div class="section" id="exercises">
15<h1><a class="toc-backref" href="./tutorial-metafunctions.html#id60" name="exercises">Exercises</a></h1>
16<dl>
17<dt>1-0.</dt>
18<dd>Use <tt class="literal"><span class="pre">BOOST_STATIC_ASSERT</span></tt> to add error checking to the <tt class="literal"><span class="pre">binary</span></tt>
19template presented in section <a class="reference" href="./resources.html">1</a>.4.1 so
20that <tt class="literal"><span class="pre">binary&lt;N&gt;::value</span></tt> causes a compilation error if <tt class="literal"><span class="pre">N</span></tt>
21contains digits other than <tt class="literal"><span class="pre">0</span></tt> or <tt class="literal"><span class="pre">1</span></tt>.</dd>
22<dt>1-1.</dt>
23<dd>Turn <tt class="literal"><span class="pre">vector_c&lt;int,1,2,3&gt;</span></tt> into a type sequence with elements
24(2,3,4) using <tt class="literal"><span class="pre">transform</span></tt>.</dd>
25<dt>1-2.</dt>
26<dd>Turn <tt class="literal"><span class="pre">vector_c&lt;int,1,2,3&gt;</span></tt> into a type sequence with elements
27(1,4,9) using <tt class="literal"><span class="pre">transform</span></tt>.</dd>
28<dt>1-3.</dt>
29<dd>Turn <tt class="literal"><span class="pre">T</span></tt> into <tt class="literal"><span class="pre">T****</span></tt> by using <tt class="literal"><span class="pre">twice</span></tt> twice.</dd>
30<dt>1-4.</dt>
31<dd>Turn <tt class="literal"><span class="pre">T</span></tt> into <tt class="literal"><span class="pre">T****</span></tt> using <tt class="literal"><span class="pre">twice</span></tt> on itself.</dd>
32<dt>1-5.</dt>
33<dd><p class="first">There's still a problem with the dimensional analysis code in
34section 1.1.
35Hint:  What happens when you do:</p>
36<pre class="literal-block">
37f = f + m * a;
38</pre>
39<p>Repair this example using techniques shown in this
40chapter.</p>
41<!-- @ example.wrap('''void will_fail%s
42{ quantity<float,force> f(m*a);
43''' % ma_function_args, '}')
44stack[:0] = dimensional_analysis # stick support code in
45compile('all', expect_error = True) -->
46</dd>
47<dt>1-6.</dt>
48<dd>Build a lambda expression that has functionality equivalent to
49<tt class="literal"><span class="pre">twice</span></tt>.  Hint: <tt class="literal"><span class="pre">mpl::apply</span></tt> is a metafunction!</dd>
50<dt>1-7*.</dt>
51<dd><p class="first">What do you think would be the semantics of the following
52constructs:</p>
53<pre class="literal-block">
54typedef mpl::lambda&lt;mpl::lambda&lt;_1&gt; &gt;::type t1;
55typedef mpl::apply&lt;_1,mpl::plus&lt;_1,_2&gt; &gt;::type t2;
56typedef mpl::apply&lt;_1,std::vector&lt;int&gt; &gt;::type t3;
57typedef mpl::apply&lt;_1,std::vector&lt;_1&gt; &gt;::type t4;
58typedef mpl::apply&lt;mpl::lambda&lt;_1&gt;,std::vector&lt;int&gt; &gt;::type t5;
59typedef mpl::apply&lt;mpl::lambda&lt;_1&gt;,std::vector&lt;_1&gt; &gt;::type t6;
60typedef mpl::apply&lt;mpl::lambda&lt;_1&gt;,mpl::plus&lt;_1,_2&gt; &gt;::type t7;
61typedef mpl::apply&lt;_1,mpl::lambda&lt; mpl::plus&lt;_1,_2&gt; &gt; &gt;::type t8;
62</pre>
63<!-- @example.prepend('#include <vector>')
64compile() -->
65<p class="last">Show the steps used to
66arrive at your answers and write tests verifying your assumptions.
67Did the library behavior match your reasoning? If not, analyze the
68failed tests to discover the actual expression semantics.
69Explain why your assumptions were different, what
70behavior you find more coherent, and why.</p>
71</dd>
72<dt>1-8*.</dt>
73<dd>Our dimensional analysis framework dealt with dimensions, but it
74entirely ignored the issue of <em>units</em>.  A length can be
75represented in inches, feet, or meters.  A force can be
76represented in newtons or in kg m/sec<sup>2</sup>.  Add the
77ability to specify units and test your code.  Try to make your
78interface as syntactically friendly as possible for the user.</dd>
79</dl>
80<!-- Along with Alan Turing, Church was one of the founders of computer
81science. -->
82<!-- yes, this is easier to read, but the real beauty of ``apply`` is that
83it not only works on metafunction classes, but on metafunctions passed
84with placeholder arguments.   -->
85<!-- Naturally, MPL defines a series of these numbered placeholder
86types, so that we can handle metafunctions with more arguments. -->
87<!-- : in order to pass a template in  the ``BinaryOperation`` position,
88``transform`` would have to be declared::
89
90 template <
91     class Sequence1, class Sequence2
92   , template <class,class> class BinaryOperation
93 >
94 struct transform;  // returning a sequence -->
95<!-- "Programming with types" -->
96<!-- Once again, a nested ``value`` member is optional for
97metafunctions, and required for integral type wrappers. -->
98</div>
99
100<div class="footer-separator"></div>
101<table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./details.html" class="navigation-link">Prev</a>&nbsp;<a href="./reference-manual.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./details.html" class="navigation-link">Back</a>&nbsp;Along</span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial-metafunctions.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>
102</tr></table></body>
103</html>
Note: See TracBrowser for help on using the repository browser.