exercises.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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>&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>
  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&lt;N&gt;::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&lt;int,1,2,3&gt;</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&lt;int,1,2,3&gt;</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&lt;mpl::lambda&lt;_1&gt; &gt;::type t1;
  58. typedef mpl::apply&lt;_1,mpl::plus&lt;_1,_2&gt; &gt;::type t2;
  59. typedef mpl::apply&lt;_1,std::vector&lt;int&gt; &gt;::type t3;
  60. typedef mpl::apply&lt;_1,std::vector&lt;_1&gt; &gt;::type t4;
  61. typedef mpl::apply&lt;mpl::lambda&lt;_1&gt;,std::vector&lt;int&gt; &gt;::type t5;
  62. typedef mpl::apply&lt;mpl::lambda&lt;_1&gt;,std::vector&lt;_1&gt; &gt;::type t6;
  63. typedef mpl::apply&lt;mpl::lambda&lt;_1&gt;,mpl::plus&lt;_1,_2&gt; &gt;::type t7;
  64. typedef mpl::apply&lt;_1,mpl::lambda&lt; mpl::plus&lt;_1,_2&gt; &gt; &gt;::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. template <
  93. class Sequence1, class Sequence2
  94. , template <class,class> class BinaryOperation
  95. >
  96. struct transform; // returning a sequence -->
  97. <!-- "Programming with types" -->
  98. <!-- Once again, a nested ``value`` member is optional for
  99. metafunctions, and required for integral type wrappers. -->
  100. </div>
  101. <div class="footer-separator"></div>
  102. <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>
  103. </tr></table></body>
  104. </html>