techniques.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <html>
  2. <head>
  3. <title>techniques.html</title>
  4. <link rel="stylesheet" type="text/css" href="../styles.css">
  5. <style>
  6. u { font-weight: normal; text-decoration: none; }
  7. </style>
  8. </head>
  9. <body>
  10. <h4>Techniques</h4>
  11. <div>
  12. The preprocessor metaprogramming techniques are presented in example format.
  13. </div>
  14. <h4>Example<u> - Use a local macro to avoid small scale repetition.</u></h4>
  15. <div class="code"><pre>
  16. #define BOOST_PP_DEF(op) /* ..................................... */ \
  17. template&lt;class T, int n&gt; \
  18. vec&lt;T, n&gt; operator op ## =(vec&lt;T, n&gt; lhs, const vec&lt;T, n&gt;& rhs) { \
  19. for (int i = 0; i &lt; n; ++i) { \
  20. lhs(i) op ## = rhs(i); \
  21. } \
  22. } \
  23. /**/
  24. BOOST_PP_DEF(+)
  25. BOOST_PP_DEF(-)
  26. BOOST_PP_DEF(*)
  27. BOOST_PP_DEF(/)
  28. #undef BOOST_PP_DEF
  29. </pre></div>
  30. <div>
  31. <b>Tip:</b>&nbsp; It is usually okay to use a standard macro name like <code>BOOST_PP_DEF</code> for this kind of code
  32. because the macro is both defined and undefined in the immediate site of its use.
  33. </div>
  34. <div>
  35. <b>Tip:</b>&nbsp; It is easier to verify proper use of the line continuation operator when they are aligned.
  36. </div>
  37. <div>
  38. <b>Notes:</b>&nbsp; You can extend this example by defining more and different kinds of operators.&nbsp;
  39. Before doing so, consider using the <i>algebraic categories</i> technique introduced in <a href="../bibliography.html#barton">[Barton]</a>
  40. or a <i>layered architecture</i> (see for instance <a href="../bibliography.html#czarnecki">[Czarnecki]</a>).&nbsp;
  41. However, at some point you must type the operator tokens <code>*</code>, <code>/</code>, <code>+</code>, <code>-</code>, etc.,
  42. because it is impossible to generate them using templates.&nbsp;
  43. The resulting <i>categorical repetition</i> of tokens can be eliminated by using preprocessor metaprogramming.
  44. </div>
  45. <h4>Example<u> - Use BOOST_PP_EMPTY as an unused parameter in local macro instantiations.</u></h4>
  46. <div class="code"><pre>
  47. #define BOOST_PP_DEF(cv) /* ... */ \
  48. template&lt;class base&gt; \
  49. cv() typename implement_subscript_using_begin_subscript&lt;base&gt;::value_type& \
  50. implement_subscript_using_begin_subscript&lt;base&gt;::operator[](index_type i) cv() { \
  51. return base::begin()[i]; \
  52. } \
  53. /**/
  54. BOOST_PP_DEF(BOOST_PP_EMPTY)
  55. BOOST_PP_DEF(BOOST_PP_IDENTITY(const))
  56. </pre></div>
  57. <div>
  58. <b>How:</b>&nbsp; BOOST_PP_EMPTY() expands to nothing and can be used as an unused parameter.
  59. </div>
  60. <div>
  61. <b>Note:</b>&nbsp; BOOST_PP_EMPTY with the () never gets expanded.&nbsp;
  62. The () is necessary to invoke a function-like macro.
  63. </div>
  64. <div>
  65. <b>Caveat:</b>&nbsp; You cannot safely use concatenation while using BOOST_PP_EMPTY().
  66. </div>
  67. <div>
  68. <b>Tip:</b>&nbsp; Occasionally, one or two lines are considerably longer than the rest.&nbsp;
  69. It can often save some work to <i>not</i> align all the line continuation operators without making the code too unreadable.
  70. </div>
  71. <div>
  72. <b>Tip:</b>&nbsp; Use syntax highlighting on preprocessor metaprogramming macro identifiers such as:
  73. <ul>
  74. <li>BOOST_PP_DEF</li>
  75. <li>BOOST_PP_EMPTY</li>
  76. <li>BOOST_PP_REPEAT</li>
  77. <li>...</li>
  78. </ul>
  79. It can greatly improve readability.
  80. </div>
  81. <h4>Example<u> - Use BOOST_PP_CAT instead of ## when necessary.</u></h4>
  82. <div class="code"><pre>
  83. #define STATIC_ASSERT(expr) \
  84. enum { BOOST_PP_CAT(static_check_, __LINE__) = (expr) ? 1 : -1 }; \
  85. typedef char \
  86. BOOST_PP_CAT(static_assert_, __LINE__)[BOOST_PP_CAT(static_check_, __LINE__)] \
  87. /**/
  88. // ...
  89. STATIC_ASSERT(sizeof(int) &lt;= sizeof(long));
  90. </pre></div>
  91. <div>
  92. <b>Why:</b>&nbsp; Macro expansion proceeds recursively in "layers."&nbsp;
  93. Token pasting prevents the preprocessor from performing macro expansion,
  94. therefore it is often necessary to delay token concatenation.
  95. </div>
  96. <h4>Example<u> - Use BOOST_PP_STRINGIZE instead of # whenever necessary.</u></h4>
  97. <div class="code"><pre>
  98. #define NOTE(str) \
  99. message(__FILE__ "(" BOOST_PP_STRINGIZE(__LINE__) ") : " str) \
  100. /**/
  101. // ...
  102. #pragma NOTE("TBD!")
  103. </pre></div>
  104. <div>
  105. <b>Why:</b>&nbsp; Macro expansion proceeds recursively in "layers."&nbsp;
  106. Stringization prevents the preprocessor from performing macro expansion, therefore it is often necessary to delay stringization.
  107. </div>
  108. <h4>Example<u> - Use BOOST_PP_ENUM_PARAMS (and its variants) or BOOST_PP_REPEAT and BOOST_PP_COMMA_IF to avoid <i>O</i>(<i>n</i>) repetition on lists in general.</u></h4>
  109. <div class="code"><pre>
  110. struct make_type_list_end;
  111. template&lt;
  112. BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(
  113. MAKE_TYPE_LIST_MAX_LENGTH,
  114. class T,
  115. make_type_list_end
  116. )
  117. &gt;
  118. struct make_type_list {
  119. private:
  120. enum { end = is_same&lt;T0, make_type_list_end&gt;::value };
  121. public:
  122. typedef typename type_if&lt;
  123. end, type_cons_empty,
  124. type_cons&lt;
  125. T0,
  126. typename type_inner_if&lt;
  127. end, type_identity&lt;end&gt;,
  128. make_type_list&lt;
  129. BOOST_PP_ENUM_SHIFTED_PARAMS(
  130. MAKE_TYPE_LIST_MAX_LENGTH,
  131. T
  132. )
  133. &gt;
  134. &gt;::type
  135. &gt;
  136. &gt;::type type;
  137. };
  138. </pre></div>
  139. <div>
  140. <b>How:</b>&nbsp; BOOST_PP_REPEAT uses simulated recursion (pseudo code):
  141. </div>
  142. <div><pre>
  143. #define BOOST_PP_REPEAT(n, m, p) BOOST_PP_REPEAT ## n(m, p)
  144. #define BOOST_PP_REPEAT0(m, p)
  145. #define BOOST_PP_REPEAT1(m, p) m(0, p)
  146. #define BOOST_PP_REPEAT2(m, p) m(0, p) m(1, p)
  147. #define BOOST_PP_REPEAT3(m, p) BOOST_PP_REPEAT2(m, p) m(2, p)
  148. #define BOOST_PP_REPEAT4(m, p) BOOST_PP_REPEAT3(m, p) m(3, p)
  149. // ...
  150. </pre></div>
  151. <div>
  152. <i>Note:&nbsp; This is no longer how BOOST_PP_REPEAT is implemented, so the code above is illustrative only!&nbsp;</i>
  153. </div>
  154. <div>
  155. BOOST_PP_ENUM_PARAMS and its variations use BOOST_PP_REPEAT.&nbsp;
  156. BOOST_PP_COMMA_IF(I) expands to a comma if I != 0.&nbsp;
  157. BOOST_PP_INC(I) essentially expands to "I+1," and BOOST_PP_DEC(I) essentially expands to "I-1.".
  158. </div>
  159. <h4>Example<u> - Use a <i>conditional macro definition</i> to enable user configuration of code repetition based on need rather than some "reasonable" upper limit.</u></h4>
  160. <div class="code"><pre>
  161. #ifndef MAKE_TYPE_LIST_MAX_LENGTH
  162. #define MAKE_TYPE_LIST_MAX_LENGTH 8
  163. #endif
  164. </pre></div>
  165. <div>
  166. Now the user can configure the <code>make_type_list</code> primitive without modifying library code.
  167. </div>
  168. <h4>Example<u> - Use BOOST_PP_REPEAT and a <i>token look-up function</i> to eliminate categorical repetition.</u></h4>
  169. <div class="code"><pre>
  170. // CAVEAT: My compiler is not standard on arithmetic types.
  171. #define ARITHMETIC_TYPE(I) ARITHMETIC_TYPE ## I
  172. #define ARITHMETIC_TYPE0 bool
  173. #define ARITHMETIC_TYPE1 char
  174. #define ARITHMETIC_TYPE2 signed char
  175. #define ARITHMETIC_TYPE3 unsigned char
  176. #define ARITHMETIC_TYPE4 short
  177. #define ARITHMETIC_TYPE5 unsigned short
  178. #define ARITHMETIC_TYPE6 int
  179. #define ARITHMETIC_TYPE7 unsigned int
  180. #define ARITHMETIC_TYPE8 long
  181. #define ARITHMETIC_TYPE9 unsigned long
  182. #define ARITHMETIC_TYPE10 float
  183. #define ARITHMETIC_TYPE11 double
  184. #define ARITHMETIC_TYPE12 long double
  185. #define ARITHMETIC_TYPE_CNT 13
  186. // ...
  187. #define BOOST_PP_DEF(z, I, _) /* ... */ \
  188. catch (ARITHMETIC_TYPE(I) t) { \
  189. report_typeid(t); \
  190. report_value(t); \
  191. } \
  192. /**/
  193. BOOST_PP_REPEAT(ARITHMETIC_TYPE_CNT, BOOST_PP_DEF, _)
  194. #undef BOOST_PP_DEF
  195. </pre></div>
  196. <div>
  197. <b>Note:</b>&nbsp; The repetition of the above example can be eliminated using template metaprogramming <a href="../bibliography.html#czarnecki">[Czarnecki]</a> as well.&nbsp;
  198. However categorical repetition of operator tokens cannot be completely eliminated by using template metaprogramming.
  199. </div>
  200. <h4>Example<u> - Use BOOST_PP_REPEAT to avoid <i>O</i>(<i>n</i>*<i>n</i>) repetition.</u></h4>
  201. <div class="code"><pre>
  202. #ifndef MAX_VEC_ARG_CNT
  203. #define MAX_VEC_ARG_CNT 8
  204. #endif
  205. // ...
  206. #define ARG_FUN(z, i, _) BOOST_PP_COMMA_IF(i) T a ## i
  207. #define ASSIGN_FUN(z, i, ) (*this)[i] = a ## i;
  208. #define DEF_VEC_CTOR_FUN(z, i, _) /* ... */ \
  209. vec(BOOST_PP_REPEAT(i, ARG_FUN, _)) { \
  210. BOOST_PP_REPEAT(i, ASSIGN_FUN, _) \
  211. } \
  212. /**/
  213. BOOST_PP_REPEAT(BOOST_PP_INC(MAX_VEC_ARG_CNT), DEF_VEC_CTOR_FUN, _)
  214. #undef ARG_FUN
  215. #undef ASSIGN_FUN
  216. #undef DEF_VEC_CTOR_FUN
  217. // ...
  218. </pre></div>
  219. <div>
  220. <b>How:</b>&nbsp; BOOST_PP_REPEAT is implemented is a special way to enable <i>automatic recursion</i>.
  221. </div>
  222. <h4>Example<u> - Use BOOST_PP_IF to implement special case for the first element.</u></h4>
  223. <div class="code"><pre>
  224. #define COMMA_IF(c) \
  225. BOOST_PP_IF(c, BOOST_PP_COMMA, BOOST_PP_EMPTY)() \
  226. /**/
  227. BOOST_PP_IF(0, true, false) == false;
  228. BOOST_PP_IF(1, true, false) == true;
  229. </pre></div>
  230. <div>
  231. BOOST_PP_IF enables convenient generation of lists using BOOST_PP_REPEAT.
  232. </div>
  233. <div>
  234. <b>Note:</b>&nbsp; <i>THEN</i> and <i>ELSE</i> don't have to be macros.&nbsp;
  235. However, if at least one of them is a function-like macro, and you want it be expanded conditionally,
  236. you have to make the other parameter a function-like macro too.&nbsp;
  237. This can often be done using BOOST_PP_IDENTITY.&nbsp;
  238. Consider the following example (by Aleksey Gurtovoy):
  239. </div>
  240. <div><pre>
  241. #define NUMBERED_EXPRESSION(i, x) /* ... */ \
  242. BOOST_PP_IF( \
  243. i, \
  244. BOOST_PP_IDENTITY(x ## i) \
  245. BOOST_PP_EMPTY \
  246. )() \
  247. /**/
  248. </pre></div>
  249. <div>
  250. <b>Note:</b>&nbsp; Like in the above implementation of COMMA_IF, the result of BOOST_PP_IF is often invoked and not the <i>THEN</i> and <i>ELSE</i> parameters.&nbsp;
  251. If the parameters were invoked, the code would not expand correctly, because the BOOST_PP_EMPTY parameter would get expanded
  252. to nothing before the <b>BOOST_PP_IF</b> would be properly expanded.
  253. </div>
  254. <div>
  255. <b>How:</b>&nbsp; BOOST_PP_IF is defined for the entire repeat range (psuedo code):
  256. </div>
  257. <div><pre>
  258. #define BOOST_PP_IF(c, THEN, ELSE) BOOST_PP_IF ## c(THEN, ELSE)
  259. #define BOOST_PP_IF0(THEN, ELSE) ELSE
  260. #define BOOST_PP_IF1(THEN, ELSE) THEN
  261. #define BOOST_PP_IF1(THEN, ELSE) THEN
  262. // ...
  263. </pre></div>
  264. <h4>Example:<u>&nbsp; Use arithmetic, logical, and comparison operations when necessary.</u></h4>
  265. <div class="code"><pre>
  266. #define SPECIAL_NUMBERED_LIST(n, i, elem, special) \
  267. BOOST_PP_ASSERT_MSG( \
  268. BOOST_PP_LESS(i, n), \
  269. bad params for SPECIAL_NUMBERED_LIST! \
  270. ) \
  271. BOOST_PP_ENUM_PARAMS(i, elem) \
  272. BOOST_PP_COMMA_IF(i) special \
  273. BOOST_PP_REPEAT( \
  274. BOOST_PP_SUB(BOOST_PP_DEC(n), i), \
  275. SPECIAL_NUMBERED_LIST_HELPER, \
  276. (elem, i) \
  277. ) \
  278. /**/
  279. #define SPECIAL_NUMBERED_LIST_HELPER(z, i, elem_base) \
  280. , \
  281. BOOST_PP_CAT( \
  282. BOOST_PP_TUPLE_ELEM(2, 0, elem_base), \
  283. BOOST_PP_ADD( \
  284. i, \
  285. BOOST_PP_TUPLE_ELEM(2, 1, elem_base) \
  286. ) \
  287. ) \
  288. /**/
  289. SPECIAL_NUMBERED_LIST(3, 0, E, S)
  290. SPECIAL_NUMBERED_LIST(3, 1, E, S)
  291. SPECIAL_NUMBERED_LIST(3, 2, E, S)
  292. SPECIAL_NUMBERED_LIST(3, 3, E, S)
  293. </pre></div>
  294. <hr size="1">
  295. <div style="margin-left: 0px;">
  296. <i>© Copyright <a href="http://www.housemarque.com" target="_top">Housemarque Oy</a> 2002</i>
  297. </div>
  298. <div style="margin-left: 0px;">
  299. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies.&nbsp;
  300. This document is provided "as is" without express or implied warranty and with no claim as to its suitability for any purpose.
  301. </div>
  302. <hr size="1">
  303. <div style="margin-left: 0px;">
  304. <i>© Copyright <a href="http://www.housemarque.com" target="_top">Housemarque Oy</a> 2002</i>
  305. </br><i>© Copyright Paul Mensonides 2002</i>
  306. </div>
  307. <div style="margin-left: 0px;">
  308. <p><small>Distributed under the Boost Software License, Version 1.0. (See
  309. accompanying file <a href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or
  310. copy at <a href=
  311. "http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</small></p>
  312. </div>
  313. </body>
  314. </html>