local_iteration.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <html>
  2. <head>
  3. <title>local_iteration.html</title>
  4. <link rel="stylesheet" type="text/css" href="../styles.css">
  5. </head>
  6. <body>
  7. <h4>Local Iteration</h4>
  8. <div>
  9. Local iteration is a simple vertical repetition construct.&nbsp;
  10. It expands a macro with each number in a user-specified range.&nbsp;
  11. Each expansion is on a separate line.
  12. </div>
  13. <h4>Tutorial</h4>
  14. <div>
  15. This mechanism requires two pieces of information to operate:&nbsp;
  16. a range to iterate over and a macro to expand on each iteration.&nbsp;
  17. This information is obtained by the mechanism through two <i>named external arguments</i>.&nbsp;
  18. These arguments are specified as user-defined macros named <b>BOOST_PP_LOCAL_LIMITS</b> and <b>BOOST_PP_LOCAL_MACRO</b>.
  19. </div>
  20. <div>
  21. <b>BOOST_PP_LOCAL_LIMITS</b> specifies a range of values to iterate over.&nbsp;
  22. It <i>must</i> expand to a <i>tuple</i> containing two elements--a lower and upper bound.&nbsp;
  23. Both the upper and lower bounds must be numeric values in the range of <i>0</i> to <b>BOOST_PP_LIMIT_ITERATION</b>.&nbsp;
  24. For example, if the user wishes a macro to be expanded with numbers ranging from <i>0</i> to <i>10</i>,
  25. <b>BOOST_PP_LOCAL_LIMITS</b> would be defined like this:
  26. </div>
  27. <div class="code"><pre>
  28. #define BOOST_PP_LOCAL_LIMITS (0, 10)
  29. </pre></div>
  30. <div>
  31. Note that there is whitespace after the name of the macro.&nbsp;
  32. The macro <i>does not</i> take <i>two</i> arguments.&nbsp;
  33. In the case above, if there was no whitespace, a preprocessing error would occur because <i>0</i> and <i>10</i> are invalid identifiers.
  34. </div>
  35. <div>
  36. Both the upper and lower bounds specified in the <b>BOOST_PP_LOCAL_LIMITS</b> macro are <i>evaluated parameters</i>.&nbsp;
  37. This implies that they can include simple arithmetic or logical expressions.&nbsp;
  38. For instance, the above definition could easily have been written like this:
  39. </div>
  40. <div class="code"><pre>
  41. #define N() 5
  42. #define BOOST_PP_LOCAL_LIMITS (0, N() + 5)
  43. </pre></div>
  44. <div>
  45. Because of this, if the whitespace after the macro name is elided, it is possible for the definition to be syntactically valid:
  46. </div>
  47. <div class="code"><pre>
  48. #define A 0
  49. #define B 10
  50. #define BOOST_PP_LOCAL_LIMITS(A, B)
  51. // note: no whitespace ^
  52. </pre></div>
  53. <div>
  54. If this happens, an error will occur inside the mechanism when it attempts to use this macro.&nbsp;
  55. The error messages that result may be obscure, so always remember to include the whitespace.&nbsp;
  56. A <i>correct</i> version of the above looks like this:
  57. </div>
  58. <div class="code"><pre>
  59. #define A 0
  60. #define B 10
  61. #define BOOST_PP_LOCAL_LIMITS (A, B)
  62. // note: has whitespace ^
  63. </pre></div>
  64. <div>
  65. <b>BOOST_PP_LOCAL_MACRO</b> is the macro that is expanded by the mechanism.&nbsp;
  66. This macro is expanded on each iteration with the current number of the iteration.&nbsp;
  67. It must defined as a unary macro <i>or</i> result in a macro that can be called with one argument:
  68. </div>
  69. <div class="code"><pre>
  70. #define BOOST_PP_LOCAL_MACRO(n) \
  71. template&lt;&gt; struct sample&lt;n&gt; { }; \
  72. /**/
  73. </pre></div>
  74. <div>
  75. ...or...
  76. </div>
  77. <div class="code"><pre>
  78. #define SAMPLE(n) \
  79. template&lt;&gt; struct sample&lt;n&gt; { }; \
  80. /**/
  81. #define BOOST_PP_LOCAL_MACRO SAMPLE
  82. </pre></div>
  83. <div>
  84. Once these two macros are defined, the local iteration is initiated by <i>including</i> <b>BOOST_PP_LOCAL_ITERATE</b>().
  85. </div>
  86. <div class="code"><pre>
  87. ??=include BOOST_PP_LOCAL_ITERATE()
  88. </pre></div>
  89. <div>
  90. (The <code>??=</code> token is a trigraph for <code>#</code>.&nbsp;
  91. I use the trigraph to make it clear that I am <i>including</i> a file rather than defining or expanding a macro, but it is not necessary.&nbsp;
  92. Even the digraph version, <code>%:</code>, could be used.&nbsp;
  93. Some compilers do not readily accept trigraphs and digraphs, so keep that in mind.&nbsp;
  94. Other than that, use whichever one you prefer.)
  95. </div>
  96. <div>
  97. In order to repeat the <code>sample</code> specialization, the pieces must be put together....
  98. </div>
  99. <div class="code"><pre>
  100. #define BOOST_PP_LOCAL_MACRO(n) \
  101. template&lt;&gt; struct sample&lt;n&gt; { }; \
  102. /**/
  103. #define BOOST_PP_LOCAL_LIMITS (0, 10)
  104. ??=include BOOST_PP_LOCAL_ITERATE()
  105. </pre></div>
  106. <div>
  107. This will result in a specialization of <code>sample</code> for each number in the range of <i>0</i> to <i>10</i>.&nbsp;
  108. The output will look something like this:
  109. </div>
  110. <div class="code"><pre>
  111. template&lt;&gt; struct sample&lt;0&gt; { };
  112. template&lt;&gt; struct sample&lt;1&gt; { };
  113. template&lt;&gt; struct sample&lt;2&gt; { };
  114. // ...
  115. template&lt;&gt; struct sample&lt;10&gt; { };
  116. </pre></div>
  117. <div>
  118. After the local-iteration is complete, both <b>BOOST_PP_LOCAL_LIMITS</b> and <b>BOOST_PP_LOCAL_MACRO</b> are automatically undefined.&nbsp;
  119. If the values need to be retained for a future local-iteration, they must be defined indirectly:
  120. </div>
  121. <div class="code"><pre>
  122. #define LIMITS (0, 10)
  123. #define SAMPLE(n) \
  124. template&lt;&gt; struct sample&lt;n&gt; { }; \
  125. /**/
  126. #define BOOST_PP_LOCAL_LIMITS LIMITS
  127. #define BOOST_PP_LOCAL_MACRO(n) SAMPLE(n)
  128. ??=include BOOST_PP_LOCAL_ITERATE()
  129. </pre></div>
  130. <h4>See Also</h4>
  131. <ul>
  132. <li><a href="../ref/local_iterate.html">BOOST_PP_LOCAL_ITERATE</a></li>
  133. <li><a href="../ref/local_limits.html">BOOST_PP_LOCAL_LIMITS</a></li>
  134. <li><a href="../ref/local_macro.html">BOOST_PP_LOCAL_MACRO</a></li>
  135. </ul>
  136. <div class="sig">- Paul Mensonides</div>
  137. <hr size="1">
  138. <div style="margin-left: 0px;">
  139. <i>© Copyright <a href="http://www.housemarque.com" target="_top">Housemarque Oy</a> 2002</i>
  140. </br><i>© Copyright Paul Mensonides 2002</i>
  141. </div>
  142. <div style="margin-left: 0px;">
  143. <p><small>Distributed under the Boost Software License, Version 1.0. (See
  144. accompanying file <a href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
  145. copy at <a href=
  146. "http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</small></p>
  147. </div>
  148. </body>
  149. </html>