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