AppendixA-AnIntroductiontoPreprocessorMetaprogramming.html 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494
  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"><html
  3. lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  4. <head>
  5. <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
  6. <meta name="generator" content="Docutils 0.3.8: http://docutils.sourceforge.net/" />
  7. <title>Appendix A - An Introduction to Preprocessor Metaprogramming</title>
  8. <meta name="copyright" content="From &quot;C++ Template Metaprogramming,&quot; by David Abrahams and Aleksey Gurtovoy. Copyright (c) 2005 by Pearson Education, Inc. Reprinted with permission." />
  9. <style type="text/css">
  10. /*
  11. :Author: David Goodger
  12. :Contact: goodger@users.sourceforge.net
  13. :date: $Date: 2004/06/23 13:31:26 $
  14. :version: $Revision: 1.37 $
  15. :copyright: This stylesheet has been placed in the public domain.
  16. Default cascading style sheet for the HTML output of Docutils.
  17. */
  18. .first {
  19. margin-top: 0 }
  20. .last {
  21. margin-bottom: 0 }
  22. a.toc-backref {
  23. text-decoration: none ;
  24. color: black }
  25. blockquote.epigraph {
  26. margin: 2em 5em ; }
  27. dd {
  28. margin-bottom: 0.5em }
  29. /* Uncomment (& remove this text!) to get bold-faced definition list terms
  30. dt {
  31. font-weight: bold }
  32. */
  33. div.abstract {
  34. margin: 2em 5em }
  35. div.abstract p.topic-title {
  36. font-weight: bold ;
  37. text-align: center }
  38. div.attention, div.caution, div.danger, div.error, div.hint,
  39. div.important, div.note, div.tip, div.warning, div.admonition {
  40. margin: 2em ;
  41. border: medium outset ;
  42. padding: 1em }
  43. div.attention p.admonition-title, div.caution p.admonition-title,
  44. div.danger p.admonition-title, div.error p.admonition-title,
  45. div.warning p.admonition-title {
  46. color: red ;
  47. font-weight: bold ;
  48. font-family: sans-serif }
  49. div.hint p.admonition-title, div.important p.admonition-title,
  50. div.note p.admonition-title, div.tip p.admonition-title,
  51. div.admonition p.admonition-title {
  52. font-weight: bold ;
  53. font-family: sans-serif }
  54. div.dedication {
  55. margin: 2em 5em ;
  56. text-align: center ;
  57. font-style: italic }
  58. div.dedication p.topic-title {
  59. font-weight: bold ;
  60. font-style: normal }
  61. div.figure {
  62. margin-left: 2em }
  63. div.footer, div.header {
  64. font-size: smaller }
  65. div.sidebar {
  66. margin-left: 1em ;
  67. border: medium outset ;
  68. padding: 0em 1em ;
  69. background-color: #ffffee ;
  70. width: 40% ;
  71. float: right ;
  72. clear: right }
  73. div.sidebar p.rubric {
  74. font-family: sans-serif ;
  75. font-size: medium }
  76. div.system-messages {
  77. margin: 5em }
  78. div.system-messages h1 {
  79. color: red }
  80. div.system-message {
  81. border: medium outset ;
  82. padding: 1em }
  83. div.system-message p.system-message-title {
  84. color: red ;
  85. font-weight: bold }
  86. div.topic {
  87. margin: 2em }
  88. h1.title {
  89. text-align: center }
  90. h2.subtitle {
  91. text-align: center }
  92. hr {
  93. width: 75% }
  94. ol.simple, ul.simple {
  95. margin-bottom: 1em }
  96. ol.arabic {
  97. list-style: decimal }
  98. ol.loweralpha {
  99. list-style: lower-alpha }
  100. ol.upperalpha {
  101. list-style: upper-alpha }
  102. ol.lowerroman {
  103. list-style: lower-roman }
  104. ol.upperroman {
  105. list-style: upper-roman }
  106. p.attribution {
  107. text-align: right ;
  108. margin-left: 50% }
  109. p.caption {
  110. font-style: italic }
  111. p.credits {
  112. font-style: italic ;
  113. font-size: smaller }
  114. p.label {
  115. white-space: nowrap }
  116. p.rubric {
  117. font-weight: bold ;
  118. font-size: larger ;
  119. color: maroon ;
  120. text-align: center }
  121. p.sidebar-title {
  122. font-family: sans-serif ;
  123. font-weight: bold ;
  124. font-size: larger }
  125. p.sidebar-subtitle {
  126. font-family: sans-serif ;
  127. font-weight: bold }
  128. p.topic-title {
  129. font-weight: bold }
  130. pre.address {
  131. margin-bottom: 0 ;
  132. margin-top: 0 ;
  133. font-family: serif ;
  134. font-size: 100% }
  135. pre.line-block {
  136. font-family: serif ;
  137. font-size: 100% }
  138. pre.literal-block, pre.doctest-block {
  139. margin-left: 2em ;
  140. margin-right: 2em ;
  141. background-color: #eeeeee }
  142. span.classifier {
  143. font-family: sans-serif ;
  144. font-style: oblique }
  145. span.classifier-delimiter {
  146. font-family: sans-serif ;
  147. font-weight: bold }
  148. span.interpreted {
  149. font-family: sans-serif }
  150. span.option {
  151. white-space: nowrap }
  152. span.option-argument {
  153. font-style: italic }
  154. span.pre {
  155. white-space: pre }
  156. span.problematic {
  157. color: red }
  158. table {
  159. margin-top: 0.5em ;
  160. margin-bottom: 0.5em }
  161. table.citation {
  162. border-left: solid thin gray ;
  163. padding-left: 0.5ex }
  164. table.docinfo {
  165. margin: 2em 4em }
  166. table.footnote {
  167. border-left: solid thin black ;
  168. padding-left: 0.5ex }
  169. dt {
  170. font-weight: bold }
  171. td, th {
  172. padding-left: 0.5em ;
  173. padding-right: 0.5em ;
  174. vertical-align: top }
  175. th.docinfo-name, th.field-name {
  176. font-weight: bold ;
  177. text-align: left ;
  178. white-space: nowrap }
  179. h1 tt, h2 tt, h3 tt, h4 tt, h5 tt, h6 tt {
  180. font-size: 100% }
  181. tt {
  182. background-color: #eeeeee }
  183. ul.auto-toc {
  184. list-style-type: none }
  185. </style> </head>
  186. <body><br />
  187. <div class="document" id="preprocessor-title">
  188. <h1 class="title">Appendix A - An Introduction to Preprocessor
  189. Metaprogramming</h1>
  190. <table class="docinfo" frame="void" rules="none">
  191. <colgroup><col class="docinfo-name" /> <col class="docinfo-content" />
  192. </colgroup>
  193. <tbody valign="top">
  194. <tr>
  195. <th class="docinfo-name">Copyright:</th>
  196. <td>From "C++ Template Metaprogramming," by David Abrahams and
  197. Aleksey Gurtovoy. Copyright (c) 2005 by Pearson Education, Inc.
  198. Reprinted with permission.</td>
  199. </tr>
  200. <tr class="field">
  201. <th class="docinfo-name">ISBN:</th>
  202. <td class="field-body">0321227255</td>
  203. </tr>
  204. </tbody>
  205. </table>
  206. <div class="section" id="motivation">
  207. <h1><a name="motivation">A.1   Motivation</a></h1>
  208. <p>Even with the full power of template metaprogramming and the <a class="reference"
  209. href="http://www.boost.org/libs/mpl">Boost Metaprogramming library</a>
  210. at our disposal, some C++ coding jobs still require a great deal of
  211. boilerplate code repetition. We saw one example in Chapter 5, when we
  212. implemented <tt class="docutils literal"><span class="pre">tiny_size</span></tt>:</p>
  213. <pre class="literal-block">template &lt;class T0, class T1, class T2&gt;
  214. struct tiny_size
  215. : mpl::int_&lt;3&gt; {};
  216. </pre>
  217. <!-- : rst-mode hack -->
  218. <!-- @prefix.append('struct none {};') -->
  219. <p>Aside from the repeated pattern in the parameter list of the primary
  220. template above, there are three partial specializations below, which
  221. also follow a predictable pattern:</p>
  222. <pre class="literal-block">template &lt;class T0, class T1&gt;
  223. struct tiny_size&lt;T0,T1,none&gt;
  224. : mpl::int_&lt;2&gt; {};
  225. template &lt;class T0&gt;
  226. struct tiny_size&lt;T0,none,none&gt;
  227. : mpl::int_&lt;1&gt; {};
  228. template &lt;&gt;
  229. struct tiny_size&lt;none,none,none&gt;
  230. : mpl::int_&lt;0&gt; {};
  231. </pre>
  232. <!-- : rst-mode hack -->
  233. <!-- @compile('all') -->
  234. <p>In this case there is only a small amount of code with such a
  235. "mechanical" flavor, but had we been implementing <tt class="docutils literal"><span
  236. class="pre">large</span></tt> instead of <tt class="docutils literal"><span
  237. class="pre">tiny</span></tt>, there might easily have been a great
  238. deal more. When the number of instances of a pattern grows beyond two
  239. or three, writing them by hand tends to become error-prone. Perhaps
  240. more importantly, the code gets hard to read, because the important
  241. abstraction in the code is really the pattern, not the individual
  242. instances.</p>
  243. <div class="section" id="code-generation">
  244. <h2><a name="code-generation">A.1.1   Code Generation</a></h2>
  245. <p>Rather than being written out by hand, mechanical-looking code
  246. should really be generated mechanically. Having written a program to
  247. spit out instances of the code pattern, a library author has two
  248. choices: She can either ship pre-generated source code files, or she
  249. can ship the generator itself. Either approach has drawbacks. If
  250. clients only get the generated source, they are stuck with whatever
  251. the library author generated—and experience shows that if they are
  252. happy with three instances of a pattern today, someone will need
  253. four tomorrow. If clients get the generator program, on the other
  254. hand, they also need the resources to execute it (e.g.,
  255. interpreters), and they must integrate the generator into their
  256. build processes...</p>
  257. </div>
  258. <div class="section" id="enter-the-preprocessor">
  259. <h2><a name="enter-the-preprocessor">A.1.2   Enter the Preprocessor</a></h2>
  260. <p>...unless the generator is a preprocessor metaprogram. Though not
  261. designed for that purpose, the C and C++ preprocessors can be made
  262. to execute sophisticated programs during the preprocessing phase of
  263. compilation. Users can control the code generation process with
  264. preprocessor <tt class="docutils literal"><span class="pre">#define</span></tt>s
  265. in code or <tt class="docutils literal"><span class="pre">-D</span></tt>
  266. options on the compiler's command line, making build integration
  267. trivial. For example, we might parameterize the primary <tt class="docutils literal"><span
  268. class="pre">tiny_size</span></tt> template above as follows:</p>
  269. <pre class="literal-block">#include &lt;<strong>boost/preprocessor/repetition/enum_params</strong>.hpp&gt;
  270. #ifndef TINY_MAX_SIZE
  271. # define TINY_MAX_SIZE 3 // default maximum size is 3
  272. #endif
  273. template &lt;<strong>BOOST_PP_ENUM_PARAMS(TINY_MAX_SIZE, class T)</strong>&gt;
  274. struct tiny_size
  275. : mpl::int_&lt;TINY_MAX_SIZE&gt;
  276. {};
  277. </pre>
  278. <!-- : rst-mode hack -->
  279. <!-- @compile(pop = None) -->
  280. <p>To test the metaprogram, run your compiler in its "preprocessing"
  281. mode (usually the <tt class="docutils literal"><span class="pre">-E</span></tt>
  282. option), with the Boost root directory in your <tt class="docutils literal"><span
  283. class="pre">#include</span></tt> path. For instance:<a class="footnote-reference"
  284. href="#minusp" id="id2" name="id2">[1]</a></p>
  285. <pre class="literal-block">g++ -P -E -Ipath/to/boost_1_32_0 -I. test.cpp
  286. </pre>
  287. <!-- @ignore() -->
  288. <table class="docutils footnote" frame="void" id="minusp" rules="none">
  289. <colgroup><col class="label" /><col /></colgroup>
  290. <tbody valign="top">
  291. <tr>
  292. <td class="label"><a class="fn-backref" href="#id2" name="minusp">[1]</a></td>
  293. <td>GCC's <tt class="docutils literal"><span class="pre">-P</span></tt>
  294. option inhibits the generation of source file and line number
  295. markers in preprocessed output.</td>
  296. </tr>
  297. </tbody>
  298. </table>
  299. <p>Given the appropriate metaprograms, users would be able to adjust
  300. not only the number of parameters to <tt class="docutils literal"><span
  301. class="pre">tiny_size</span></tt>, but the maximum size of the
  302. entire <tt class="docutils literal"><span class="pre">tiny</span></tt>
  303. implementation just by <tt class="docutils literal"><span class="pre">#define</span></tt>-ing
  304. <tt class="docutils literal"><span class="pre">TINY_MAX_SIZE</span></tt>.</p>
  305. <p>The Boost Preprocessor library <a class="citation-reference" href="#mk04"
  306. id="id3" name="id3">[MK04]</a> plays a role in preprocessor
  307. metaprogramming similar to the one played by the MPL in template
  308. metaprogramming: It supplies a framework of high-level components
  309. (like <tt class="docutils literal"><span class="pre">BOOST_PP_ENUM_PARAMS</span></tt>)
  310. that make otherwise-painful metaprogramming jobs approachable. In
  311. this appendix we won't attempt to cover nitty-gritty details of how
  312. the preprocessor works, nor principles of preprocessor
  313. metaprogramming in general, nor even many details of how the
  314. Preprocessor <em>library</em> works. We <em>will</em> show you
  315. enough at a high level that you'll be able to use the library
  316. productively and learn the rest on your own.</p>
  317. <table class="docutils citation" frame="void" id="mk04" rules="none">
  318. <colgroup><col class="label" /><col /></colgroup>
  319. <tbody valign="top">
  320. <tr>
  321. <td class="label"><a class="fn-backref" href="#id3" name="mk04">[MK04]</a></td>
  322. <td>Paul Mensonides and Vesa Karvonen. "The Boost Preprocessor
  323. Library." <a class="reference" href="http://www.boost.org/libs/preprocessor">http://www.boost.org/libs/preprocessor</a>.</td>
  324. </tr>
  325. </tbody>
  326. </table>
  327. </div>
  328. </div>
  329. <div class="section" id="fundamental-abstractions-of-the-preprocessor">
  330. <h1><a name="fundamental-abstractions-of-the-preprocessor">A.2   Fundamental
  331. Abstractions of the Preprocessor</a></h1>
  332. <p>We began our discussion of template metaprogramming in Chapter 2 by
  333. describing its metadata (potential template arguments) and
  334. metafunctions (class templates). On the basis of those two fundamental
  335. abstractions, we built up the entire picture of compile-time
  336. computation covered in the rest of this book. In this section we'll
  337. lay a similar foundation for the preprocessor metaprogrammer. Some of
  338. what we cover here may be a review for you, but it's important to
  339. identify the basic concepts going into detail.</p>
  340. <div class="section" id="preprocessing-tokens">
  341. <h2><a name="preprocessing-tokens">A.2.1   Preprocessing Tokens</a></h2>
  342. <p>The fundamental unit of data in the preprocessor is the <strong>preprocessing
  343. token</strong>. Preprocessing tokens correspond roughly to the
  344. tokens you're used to working with in C++, such as identifiers,
  345. operator symbols, and literals. Technically, there are some
  346. differences between <em>preprocessing tokens</em> and regular <em>tokens</em>
  347. (see section 2 of the C++ standard for details), but they can be
  348. ignored for the purposes of this discussion. In fact, we'll be using
  349. the terms interchangeably here.</p>
  350. </div>
  351. <div class="section" id="macros">
  352. <h2><a name="macros">A.2.2   Macros</a></h2>
  353. <p>Preprocessor macros come in two flavors. <strong>Object-like
  354. macros</strong> can be defined this way:</p>
  355. <blockquote>
  356. <div class="line-block">
  357. <div class="line"><tt class="docutils literal"><span class="pre">#define</span></tt>
  358. <em>identifier</em> <em>replacement-list</em></div>
  359. </div>
  360. </blockquote>
  361. <!-- @litre_translator.line_offset -= 7 -->
  362. <p>where the <em>identifier</em> names the macro being defined, and <em>replacement-list</em>
  363. is a sequence of zero or more tokens. Where the <em>identifier</em>
  364. appears in subsequent program text, it is <strong>expanded</strong>
  365. by the preprocessor into its <em>replacement-list</em>.</p>
  366. <p><strong>Function-like macros</strong>, which act as the
  367. "metafunctions of the preprocessing phase," are defined as follows:</p>
  368. <blockquote>
  369. <div class="line-block">
  370. <div class="line"><tt class="docutils literal"><span class="pre">#define</span></tt>
  371. <em>identifier</em>(<em>a</em><sub>1</sub>, <em>a</em><sub>2</sub>,
  372. ... <em>a</em><sub>n</sub>) <em>replacement-list</em></div>
  373. </div>
  374. </blockquote>
  375. <!-- @litre_translator.line_offset -= 7 -->
  376. <p>where each <em>a</em><sub>i</sub> is an identifier naming a <strong>macro
  377. parameter</strong>. When the macro name appears in subsequent
  378. program text followed by a suitable argument list, it is expanded
  379. into its <em>replacement-list</em>, except that each argument is
  380. substituted for the corresponding parameter where it appears in the
  381. <em>replacement-list</em>.<a class="footnote-reference" href="#expansion"
  382. id="id4" name="id4">[2]</a></p>
  383. <table class="docutils footnote" frame="void" id="expansion" rules="none">
  384. <colgroup><col class="label" /><col /></colgroup>
  385. <tbody valign="top">
  386. <tr>
  387. <td class="label"><a class="fn-backref" href="#id4" name="expansion">[2]</a></td>
  388. <td>We have omitted many details of how macro expansion works.
  389. We encourage you to take a few minutes to study section 16.3
  390. of the C++ standard, which describes that process in
  391. straightforward terms.</td>
  392. </tr>
  393. </tbody>
  394. </table>
  395. </div>
  396. <div class="section" id="macro-arguments">
  397. <h2><a name="macro-arguments">A.2.3   Macro Arguments</a></h2>
  398. <div class="admonition-definition admonition">
  399. <p class="first admonition-title">Definition</p>
  400. <p>A <strong>macro argument</strong> is a nonempty sequence of:</p>
  401. <ul class="last simple">
  402. <li>Preprocessing tokens other than commas or parentheses, <em>and/or</em></li>
  403. <li>Preprocessing tokens surrounded by matched pairs of
  404. parentheses.</li>
  405. </ul>
  406. </div>
  407. <p>This definition has consequences for preprocessor metaprogramming
  408. that must not be underestimated. Note, first of all, that the
  409. following tokens have special status:</p>
  410. <blockquote>
  411. <pre class="literal-block">, ( )
  412. </pre> </blockquote>
  413. <!-- @ignore() -->
  414. <p>As a result, a macro argument can never contain an unmatched
  415. parenthesis, or a comma that is not surrounded by matched
  416. parentheses. For example, both lines following the definition of FOO
  417. below are ill-formed:</p>
  418. <pre class="literal-block">#define FOO(X) X // Unary identity macro
  419. FOO(,) // un-parenthesized comma or two empty arguments
  420. FOO()) // unmatched parenthesis or missing argument
  421. </pre>
  422. <!-- @def pp_failure(options = ['-E'], **kw):
  423. compile( expect_error = not 'mwcc' in config.compiler , options = options, **kw)pp_failure() -->
  424. <p>Note also that the following tokens do <em>not</em> have special
  425. status; the preprocessor knows nothing about matched pairs of
  426. braces, brackets, or angle brackets:</p>
  427. <blockquote>
  428. <pre class="literal-block">{ } [ ] &lt; &gt;
  429. </pre> </blockquote>
  430. <!-- @ignore() -->
  431. <p>As a result, these lines are also ill-formed:</p>
  432. <pre class="literal-block">FOO(std::pair&lt;int<strong>,</strong> long&gt;) // two arguments
  433. FOO({ int x = 1<strong>,</strong> y = 2; return x+y; }) // two arguments
  434. </pre>
  435. <!-- @example.prepend('#define FOO(X) X')
  436. pp_failure() -->
  437. <p>It <em>is</em> possible to pass either string of tokens above as
  438. part of a single macro argument, provided it is parenthesized:</p>
  439. <pre class="literal-block">FOO(<strong>(</strong>std::pair&lt;int,int&gt;<strong>)</strong>) // one argument
  440. FOO(<strong>(</strong>{ int x = 1, y = 2; return x+y; }<strong>)</strong>) // one argument
  441. </pre>
  442. <!-- @example.prepend('#define FOO(X) X')
  443. compile(options = ['-E']) -->
  444. <p>However, because of the special status of commas, it is impossible
  445. to strip parentheses from a macro argument without knowing the
  446. number of comma-separated token sequences it contains.<a class="footnote-reference"
  447. href="#c99" id="id5" name="id5">[3]</a> If you are writing a macro
  448. that needs to be able to accept an argument containing a variable
  449. number of commas, your users will either have to parenthesize that
  450. argument <em>and</em> pass you the number of comma-separated token
  451. sequences as an additional argument, or they will have to encode the
  452. same information in one of the preprocessor data structures covered
  453. later in this appendix.</p>
  454. <table class="docutils footnote" frame="void" id="c99" rules="none">
  455. <colgroup><col class="label" /><col /></colgroup>
  456. <tbody valign="top">
  457. <tr>
  458. <td class="label"><a name="c99">[3]</a></td>
  459. <td><em>(<a class="fn-backref" href="#id5">1</a>, <a class="fn-backref"
  460. href="#id12">2</a>)</em> The C99 preprocessor, by virtue
  461. of its variadic macros, can do that and more. The C++
  462. standardization committee is likely to adopt C99's
  463. preprocessor extensions for the next version of the C++
  464. standard.</td>
  465. </tr>
  466. </tbody>
  467. </table>
  468. </div>
  469. </div>
  470. <div class="section" id="preprocessor-library-structure">
  471. <h1><a name="preprocessor-library-structure">A.3   Preprocessor Library
  472. Structure</a></h1>
  473. <p>Since in-depth coverage of the Boost Preprocessor library is beyond
  474. the scope of this book, we'll try to give you the <em>tools</em> to
  475. gain an in-depth understanding of the library here. To do that, you'll
  476. need to use the electronic Preprocessor library documentation, which
  477. begins with the index.html file in the <tt class="docutils literal"><span
  478. class="pre">libs/preprocessor/</span></tt> subdirectory of your
  479. Boost installation.</p>
  480. <p>On the left of your browser window you'll see an index, and if you
  481. follow the "Headers" link, it will reveal the structure of the <tt class="docutils literal"><span
  482. class="pre">boost/preprocessor/</span></tt> directory. Most of the
  483. library's headers are grouped into subdirectories according to related
  484. functionality. The top-level directory contains only a few headers
  485. that provide general-purpose macros, along with a header for each
  486. subdirectory that simply <tt class="docutils literal"><span class="pre">#include</span></tt>s
  487. all the headers in that subdirectory. For example, <tt class="docutils literal"><span
  488. class="pre">boost/preprocessor/selection.hpp</span></tt> does
  489. nothing more than to <tt class="docutils literal"><span class="pre">#include</span></tt>
  490. the <tt class="docutils literal"><span class="pre">min.hpp</span></tt>
  491. and <tt class="docutils literal"><span class="pre">max.hpp</span></tt>
  492. headers that comprise the contents of <tt class="docutils literal"><span
  493. class="pre">boost/preprocessor/selection/</span></tt>. The headers
  494. whose names <em>don't</em> correspond to subdirectories generally
  495. declare a macro whose name is the same as the name of the header,
  496. without the extension, and with a <tt class="docutils literal"><span
  497. class="pre">BOOST_PP_</span></tt> prefix. For example, <tt class="docutils literal"><span
  498. class="pre">boost/preprocessor/selection/max.hpp</span></tt>
  499. declares <tt class="docutils literal"><span class="pre">BOOST_PP_MAX</span></tt>.</p>
  500. <p>You'll also notice that often a header will declare an additional
  501. macro with a <tt class="docutils literal"><span class="pre">_D</span></tt>,
  502. <tt class="docutils literal"><span class="pre">_R</span></tt>, or <tt
  503. class="docutils literal"><span class="pre">_Z</span></tt> suffix.<a
  504. class="footnote-reference" href="#suffix" id="id6" name="id6">[4]</a>
  505. For instance, <tt class="docutils literal"><span class="pre">boost/preprocessor/selection/max.hpp</span></tt>
  506. also declares <tt class="docutils literal"><span class="pre">BOOST_PP_MAX_D</span></tt>.
  507. For the purposes of this appendix, you should ignore those macros.
  508. Eventually you will want to understand how they can be used to
  509. optimize preprocessing speed; consult the Topics section of the
  510. library documentation under the subheading "reentrancy" for that
  511. information.</p>
  512. <table class="docutils footnote" frame="void" id="suffix" rules="none">
  513. <colgroup><col class="label" /><col /></colgroup>
  514. <tbody valign="top">
  515. <tr>
  516. <td class="label"><a class="fn-backref" href="#id6" name="suffix">[4]</a></td>
  517. <td>Macros with <tt class="docutils literal"><span class="pre">_1ST</span></tt>,
  518. <tt class="docutils literal"><span class="pre">_2ND</span></tt>,
  519. or <tt class="docutils literal"><span class="pre">_3RD</span></tt>
  520. suffixes, if they appear, should be ignored for a different
  521. reason: They are deprecated and will be removed from the library
  522. soon.</td>
  523. </tr>
  524. </tbody>
  525. </table>
  526. </div>
  527. <div class="section" id="preprocessor-library-abstractions">
  528. <h1><a name="preprocessor-library-abstractions">A.4   Preprocessor
  529. Library Abstractions</a></h1>
  530. <p>In this section we'll discuss the basic abstractions of the
  531. Preprocessor library, and give some simple examples of each.</p>
  532. <div class="section" id="repetition">
  533. <h2><a name="repetition">A.4.1   Repetition</a></h2>
  534. <p>The repeated generation of <tt class="docutils literal"><span class="pre">class</span>
  535. <span class="pre">T0</span></tt>, <tt class="docutils literal"><span
  536. class="pre">class</span> <span class="pre">T1</span></tt>... <tt
  537. class="docutils literal"><span class="pre">class</span> <span class="pre">T</span></tt><em>n</em>
  538. that we achieved using <tt class="docutils literal"><span class="pre">BOOST_PP_ENUM_PARAMS</span></tt>
  539. was a specific case of the general concept of <strong>horizontal
  540. repetition</strong>. The library also has a concept of vertical
  541. repetition, which we'll get to in a moment. Horizontal repetition
  542. macros are all found in the library's <tt class="docutils literal"><span
  543. class="pre">repetition/</span></tt> subdirectory.</p>
  544. <div class="section" id="horizontal-repetition">
  545. <h3><a name="horizontal-repetition">A.4.1.1   Horizontal Repetition</a></h3>
  546. <p>To generate the <tt class="docutils literal"><span class="pre">tiny_size</span></tt>
  547. specializations using horizontal repetition, we might write the
  548. following:</p>
  549. <pre class="literal-block">#include &lt;boost/preprocessor/repetition.hpp&gt;
  550. #include &lt;boost/preprocessor/arithmetic/sub.hpp&gt;
  551. #include &lt;boost/preprocessor/punctuation/comma_if.hpp&gt;
  552. #define TINY_print(z, n, data) data
  553. #define TINY_size(z, n, unused) \
  554. template &lt;BOOST_PP_ENUM_PARAMS(n, class T)&gt; \
  555. struct tiny_size&lt; \
  556. BOOST_PP_ENUM_PARAMS(n,T) \
  557. BOOST_PP_COMMA_IF(n) \
  558. BOOST_PP_ENUM( \
  559. BOOST_PP_SUB(TINY_MAX_SIZE,n), TINY_print, none) \
  560. &gt; \
  561. : mpl::int_&lt;n&gt; {};
  562. BOOST_PP_REPEAT(TINY_MAX_SIZE, TINY_size, ~)
  563. #undef TINY_size
  564. #undef TINY_print
  565. </pre>
  566. <!-- @import re
  567. compile('all', pop = None)example.sub('BOOST_PP_REPEAT.*', '', flags = re.DOTALL) -->
  568. <p>The code generation process is kicked off by calling <tt class="docutils literal"><span
  569. class="pre">BOOST_PP_REPEAT</span></tt>, a <strong>higher-order
  570. macro</strong> that repeatedly invokes the macro named by its
  571. second argument (<tt class="docutils literal"><span class="pre">TINY_size</span></tt>).
  572. The first argument specifies the number of repeated invocations,
  573. and the third one can be any data; it is passed on unchanged to
  574. the macro being invoked. In this case, <tt class="docutils literal"><span
  575. class="pre">TINY_size</span></tt> doesn't use that data, so
  576. the choice to pass <tt class="docutils literal"><span class="pre">~</span></tt>
  577. was arbitrary.<a class="footnote-reference" href="#markers" id="id7"
  578. name="id7">[5]</a></p>
  579. <table class="docutils footnote" frame="void" id="markers" rules="none">
  580. <colgroup><col class="label" /><col /></colgroup>
  581. <tbody valign="top">
  582. <tr>
  583. <td class="label"><a class="fn-backref" href="#id7" name="markers">[5]</a></td>
  584. <td><tt class="docutils literal"><span class="pre">~</span></tt>
  585. is not an <em>entirely</em> arbitrary choice. Both <tt class="docutils literal"><span
  586. class="pre">@</span></tt> and <tt class="docutils literal"><span
  587. class="pre">$</span></tt> might have been good choices,
  588. except that they are technically not part of the basic
  589. character set that C++ implementations are required to
  590. support. An identifier like <tt class="docutils literal"><span
  591. class="pre">ignored</span></tt> might be subject to
  592. macro expansion, leading to unexpected results.</td>
  593. </tr>
  594. </tbody>
  595. </table>
  596. <p>Each time the <tt class="docutils literal"><span class="pre">TINY_size</span></tt>
  597. macro is invoked by <tt class="docutils literal"><span class="pre">BOOST_PP_REPEAT</span></tt>,
  598. it generates a different specialization of <tt class="docutils literal"><span
  599. class="pre">tiny_size</span></tt>. The macro accepts three
  600. parameters.</p>
  601. <ul class="simple">
  602. <li><tt class="docutils literal"><span class="pre">z</span></tt>
  603. is related to the <tt class="docutils literal"><span class="pre">_Z</span></tt>
  604. macro suffix we mentioned earlier. You'll never need to use it
  605. except for optimization purposes, and can safely ignore it for
  606. now.</li>
  607. <li><tt class="docutils literal"><span class="pre">n</span></tt>
  608. is the repetition index. In repeated invocations of <tt class="docutils literal"><span
  609. class="pre">TINY_size</span></tt>, <tt class="docutils literal"><span
  610. class="pre">n</span></tt> will be <tt class="docutils literal"><span
  611. class="pre">0</span></tt>, then <tt class="docutils literal"><span
  612. class="pre">1</span></tt>, then <tt class="docutils literal"><span
  613. class="pre">2</span></tt>, and so on.</li>
  614. <li><tt class="docutils literal"><span class="pre">unused</span></tt>,
  615. in this case, will be <tt class="docutils literal"><span class="pre">~</span></tt>
  616. on each repetition. In general, the final argument to a macro
  617. invoked by <tt class="docutils literal"><span class="pre">BOOST_PP_REPEAT</span></tt>
  618. is always the same as its invoker's final argument.</li>
  619. </ul>
  620. <p>Because its <em>replacement-list</em> covers several lines, all
  621. but the last line of <tt class="docutils literal"><span class="pre">TINY_size</span></tt>
  622. is continued with a trailing backslash. The first few of those
  623. lines just invoke <tt class="docutils literal"><span class="pre">BOOST_PP_ENUM_PARAMS</span></tt>
  624. (which we already used in the primary template) to generate
  625. comma-separated lists, so each invocation of <tt class="docutils literal"><span
  626. class="pre">TINY_size</span></tt> produces something
  627. equivalent to:<a class="footnote-reference" href="#cont" id="id8"
  628. name="id8">[6]</a></p>
  629. <pre class="literal-block">template &lt;<strong>class T0, class T1, ... class T</strong><em>n-1</em>&gt;
  630. struct tiny_size&lt;
  631. <strong>T0, T1, ... T</strong><em>n-1</em>
  632. <em>...more...</em>
  633. &gt;
  634. : mpl::int_&lt;n&gt; {};
  635. </pre>
  636. <table class="docutils footnote" frame="void" id="cont" rules="none">
  637. <colgroup><col class="label" /><col /></colgroup>
  638. <tbody valign="top">
  639. <tr>
  640. <td class="label"><a class="fn-backref" href="#id8" name="cont">[6]</a></td>
  641. <td>Note that the line continuation characters <em>and</em>
  642. the newlines following them are removed by the preprocessor,
  643. so the resulting code actually appears on a single line in
  644. the preprocessed output.</td>
  645. </tr>
  646. </tbody>
  647. </table>
  648. <!-- @ignore() -->
  649. <p><tt class="docutils literal"><span class="pre">BOOST_PP_COMMA_IF</span></tt>
  650. generates a comma if its numeric argument is not <tt class="docutils literal"><span
  651. class="pre">0</span></tt>. When <tt class="docutils literal"><span
  652. class="pre">n</span></tt> is <tt class="docutils literal"><span
  653. class="pre">0</span></tt>, the list generated by the preceding
  654. line will be empty, and a leading comma directly following the <tt
  655. class="docutils literal"><span class="pre">&lt;</span></tt>
  656. character would be ill-formed.</p>
  657. <p>The next line uses <tt class="docutils literal"><span class="pre">BOOST_PP_ENUM</span></tt>
  658. to generate <tt class="docutils literal"><span class="pre">TINY_MAX_SIZE-n</span></tt>
  659. comma-separated copies of <tt class="docutils literal"><span class="pre">none</span></tt>.
  660. <tt class="docutils literal"><span class="pre">BOOST_PP_ENUM</span></tt>
  661. is just like <tt class="docutils literal"><span class="pre">BOOST_PP_REPEAT</span></tt>
  662. except that it generates commas between repetitions, so its second
  663. argument (<tt class="docutils literal"><span class="pre">TINY_print</span></tt>,
  664. here) must have the same signature as <tt class="docutils literal"><span
  665. class="pre">TINY_size</span></tt>. In this case, <tt class="docutils literal"><span
  666. class="pre">TINY_print</span></tt> ignores its repetition
  667. index <tt class="docutils literal"><span class="pre">n</span></tt>,
  668. and simply yields its third argument, <tt class="docutils literal"><span
  669. class="pre">none</span></tt>.</p>
  670. <p><tt class="docutils literal"><span class="pre">BOOST_PP_SUB</span></tt>
  671. implements token subtraction. It's crucial to understand that
  672. although the preprocessor <em>itself</em> can evaluate ordinary
  673. arithmetic expressions:</p>
  674. <pre class="literal-block">#define X 3
  675. ...
  676. #if <strong>X - 1 &gt; 0</strong> // OK
  677. <em>whatever</em>
  678. #endif
  679. </pre>
  680. <!-- @compile() -->
  681. <!-- @litre_translator.line_offset -= 7 -->
  682. <p>preprocessor <em>metaprograms</em> can only operate on tokens.
  683. Normally, when a macro in the Preprocessor library expects a
  684. numeric argument, it must be passed as a single token. If we had
  685. written <tt class="docutils literal"><span class="pre">TINY_MAX_SIZE-n</span></tt>
  686. instead of <tt class="docutils literal"><span class="pre">BOOST_PP_SUB(TINY_MAX_SIZE,n)</span></tt>
  687. above, the first argument to <tt class="docutils literal"><span class="pre">BOOST_PP_ENUM</span></tt>
  688. would have contained three tokens at each invocation: first <tt class="docutils literal"><span
  689. class="pre">3-0</span></tt>, then <tt class="docutils literal"><span
  690. class="pre">3-1</span></tt>, and finally <tt class="docutils literal"><span
  691. class="pre">3-2</span></tt>. <tt class="docutils literal"><span
  692. class="pre">BOOST_PP_SUB</span></tt>, though, generates
  693. single-token results: first <tt class="docutils literal"><span class="pre">3</span></tt>,
  694. then <tt class="docutils literal"><span class="pre">2</span></tt>,
  695. and finally <tt class="docutils literal"><span class="pre">1</span></tt>,
  696. in successive repetitions.</p>
  697. <div class="sidebar">
  698. <p class="first sidebar-title">Naming Conventions</p>
  699. <p class="last">Note that <tt class="docutils literal"><span class="pre">TINY_print</span></tt>
  700. and <tt class="docutils literal"><span class="pre">TINY_size</span></tt>
  701. are <tt class="docutils literal"><span class="pre">#undef</span></tt>'d
  702. immediately
  703. after they're used, with no intervening <tt class="docutils literal"><span
  704. class="pre">#include</span></tt>s. They can therefore be
  705. thought of as "local" macro definitions. Because the
  706. preprocessor doesn't respect scope boundaries, it's important to
  707. choose names carefully to prevent clashes. We recommend <tt class="docutils literal"><span
  708. class="pre">PREFIXED_lower_case</span></tt> names for local
  709. macros and <tt class="docutils literal"><span class="pre">PREFIXED_UPPER_CASE</span></tt>
  710. names for global ones. The only exceptions are one-letter
  711. lowercase names, which are safe to use for local macros: No
  712. other header is likely to <tt class="docutils literal"><span class="pre">#define</span></tt>
  713. a global single-letter lowercase macro—that would be <em>very</em>
  714. bad manners.</p>
  715. </div>
  716. </div>
  717. <div class="section" id="vertical-repetition">
  718. <h3><a name="vertical-repetition">A.4.1.2   Vertical Repetition</a></h3>
  719. <p>If you send the previous example through your preprocessor,
  720. you'll see one long line containing something like this:</p>
  721. <pre class="literal-block">template &lt;&gt; struct tiny_size&lt; none , none , none &gt; : mpl::int_&lt;0&gt;
  722. {}; template &lt; class T0&gt; struct tiny_size&lt; T0 , none , none &gt; :
  723. mpl::int_&lt;1&gt; {}; template &lt; class T0 , class T1&gt; struct tiny_size
  724. &lt; T0 , T1 , none &gt; : mpl::int_&lt;2&gt; {};
  725. </pre>
  726. <!-- @compile('all', pop = 1) -->
  727. <p>The distinguishing feature of horizontal repetition is that all
  728. instances of the repeated pattern are generated on the same line
  729. of preprocessed output. For some jobs, like generating the primary
  730. <tt class="docutils literal"><span class="pre">tiny_size</span></tt>
  731. template, that's perfectly appropriate. In this case, however,
  732. there are at least two disadvantages.</p>
  733. <ol class="arabic simple">
  734. <li>It's hard to verify that our metaprogram is doing the right
  735. thing without reformatting the resulting code by hand.</li>
  736. <li>The efficiency of nested horizontal repetitions varies widely
  737. across preprocessors. Each specialization generated by means of
  738. horizontal repetition contains three other horizontal
  739. repetitions: two invocations of <tt class="docutils literal"><span
  740. class="pre">BOOST_PP_ENUM_PARAMS</span></tt> and one
  741. invocation of <tt class="docutils literal"><span class="pre">BOOST_PP_ENUM</span></tt>.
  742. When <tt class="docutils literal"><span class="pre">TINY_MAX_SIZE</span></tt>
  743. is <tt class="docutils literal"><span class="pre">3</span></tt>,
  744. you'll probably never care, but on at least one preprocessor
  745. still in use today, compilation begins to slow noticeably when <tt
  746. class="docutils literal"><span class="pre">TINY_MAX_SIZE</span></tt>
  747. reaches <tt class="docutils literal"><span class="pre">8</span></tt>.<a
  748. class="footnote-reference" href="#nest" id="id9" name="id9">[7]</a></li>
  749. </ol>
  750. <blockquote>
  751. <table class="docutils footnote" frame="void" id="nest" rules="none">
  752. <colgroup><col class="label" /><col /></colgroup>
  753. <tbody valign="top">
  754. <tr>
  755. <td class="label"><a class="fn-backref" href="#id9" name="nest">[7]</a></td>
  756. <td>That said, other preprocessors can handle 256 * 256
  757. nested repetitions without any speed problems whatsoever.</td>
  758. </tr>
  759. </tbody>
  760. </table>
  761. </blockquote>
  762. <p>The solution to these problems, naturally, is <strong>vertical
  763. repetition</strong>, which generates instances of a pattern
  764. across multiple lines. The Preprocessor library provides two means
  765. of vertical repetition: <strong>local iteration</strong> and <strong>file
  766. iteration</strong>.</p>
  767. <div class="section" id="local-iteration">
  768. <h4><a name="local-iteration">Local Iteration</a></h4>
  769. <p>The most expedient way to demonstrate local iteration in our
  770. example is to replace the invocation of <tt class="docutils literal"><span
  771. class="pre">BOOST_PP_REPEAT</span></tt> with the following:</p>
  772. <pre class="literal-block">#include &lt;boost/preprocessor/<strong>iteration/local.hpp</strong>&gt;
  773. #define BOOST_PP_LOCAL_MACRO(n) TINY_size(~, n, ~)
  774. #define BOOST_PP_LOCAL_LIMITS (0, <strong>TINY_MAX_SIZE - 1</strong>)
  775. <strong>#include</strong> BOOST_PP_LOCAL_ITERATE()
  776. </pre>
  777. <!-- @compile('all', pop = 1) -->
  778. <p>Local iteration repeatedly invokes the user-defined macro with
  779. the special name <tt class="docutils literal"><span class="pre">BOOST_PP_LOCAL_MACRO</span></tt>,
  780. whose argument will be an iteration index. Since we already had
  781. <tt class="docutils literal"><span class="pre">TINY_size</span></tt>
  782. lying around, we've just defined <tt class="docutils literal"><span
  783. class="pre">BOOST_PP_LOCAL_MACRO</span></tt> to invoke it.
  784. The range of iteration indices are given by another user-defined
  785. macro, <tt class="docutils literal"><span class="pre">BOOST_PP_LOCAL_LIMITS</span></tt>,
  786. which must expand to a parenthesized pair of integer values
  787. representing the <em>inclusive</em> range of index values
  788. passed to <tt class="docutils literal"><span class="pre">BOOST_PP_LOCAL_MACRO</span></tt>.
  789. Note that this is one of the rare places where the library
  790. expects a numeric argument that can be an expression consisting
  791. of multiple tokens.</p>
  792. <p>Finally, the repetition is initiated by <tt class="docutils literal"><span
  793. class="pre">#include</span></tt>-ing the result of invoking
  794. <tt class="docutils literal"><span class="pre">BOOST_PP_LOCAL_ITERATE</span></tt>,
  795. which will ultimately be a file in the Preprocessor library
  796. itself. You may find it surprising that many preprocessors can
  797. handle repeated file inclusion more quickly than nested
  798. horizontal repetition, but that is in fact the case.</p>
  799. <p>If we throw the new example at our preprocessor, we'll see the
  800. following, on three separate lines in the output:</p>
  801. <pre class="literal-block">template &lt;&gt; struct tiny_size&lt; none , none , none &gt; : mpl::int_&lt;0&gt;
  802. {};
  803. template &lt; class T0&gt; struct tiny_size&lt; T0 , none , none &gt; : mpl::
  804. int_&lt;1&gt; {};
  805. template &lt; class T0 , class T1&gt; struct tiny_size&lt; T0 , T1 , none
  806. &gt; : mpl::int_&lt;2&gt; {};
  807. </pre>
  808. <!-- @compile('all', pop = 1) -->
  809. <p>That represents a great improvement in verifiability, but it's
  810. still not ideal. As <tt class="docutils literal"><span class="pre">TINY_MAX_SIZE</span></tt>
  811. grows, it gets harder and harder to see that the pattern is
  812. generating what we'd like. If we could get some more line breaks
  813. into the output it would retain a more recognizable form.</p>
  814. <p>Both repetition methods we've used so far have another
  815. drawback, though it doesn't show up in this example. Consider
  816. what would happen if <tt class="docutils literal"><span class="pre">tiny_size</span></tt>
  817. had a member function that we wanted to debug. If you've ever
  818. tried to use a debugger to step through a function generated by
  819. a preprocessor macro, you know that it's a frustrating
  820. experience at best: The debugger shows you the line from which
  821. the macro was ultimately invoked, which usually looks nothing at
  822. all like the code that was generated. Worse, as far as the
  823. debugger is concerned, <em>every</em> statement in that
  824. generated function occupies that same line.</p>
  825. </div>
  826. <div class="section" id="file-iteration">
  827. <h4><a name="file-iteration">File Iteration</a></h4>
  828. <p>Clearly, debuggability depends on preserving the association
  829. between generated code and the lines in the source file that
  830. describe the code pattern. File iteration generates pattern
  831. instances by repeatedly <tt class="docutils literal"><span class="pre">#include</span></tt>-ing
  832. the same source file. The effect of file iteration on
  833. debuggability is similar to that of templates: Although separate
  834. instances appear to occupy the same source lines in the
  835. debugger, we do have the experience of stepping through the
  836. function's source code.</p>
  837. <p>To apply file iteration in our example, we can replace our
  838. earlier local iteration code and the definition of <tt class="docutils literal"><span
  839. class="pre">TINY_size</span></tt>, with:</p>
  840. <pre class="literal-block">#include &lt;boost/preprocessor/iteration/iterate.hpp&gt;
  841. #define BOOST_PP_ITERATION_LIMITS (0, TINY_MAX_SIZE - 1)
  842. #define BOOST_PP_FILENAME_1 "tiny_size_spec.hpp"
  843. #include BOOST_PP_ITERATE()
  844. </pre>
  845. <p><tt class="docutils literal"><span class="pre">BOOST_PP_ITERATION_LIMITS</span></tt>
  846. follows the same pattern as <tt class="docutils literal"><span
  847. class="pre">BOOST_PP_LOCAL_LIMITS</span></tt> did, allowing
  848. us to specify an inclusive range of iteration indices. <tt class="docutils literal"><span
  849. class="pre">BOOST_PP_FILENAME_1</span></tt> specifies the
  850. name of the file to repeatedly <tt class="docutils literal"><span
  851. class="pre">#include</span></tt> (we'll show you that file
  852. in a moment). The trailing <tt class="docutils literal"><span class="pre">1</span></tt>
  853. indicates that this is the first nesting level of file
  854. iteration—should we need to invoke file iteration again from
  855. within <tt class="docutils literal"><span class="pre">tiny_size_spec.hpp</span></tt>,
  856. we'd need to use <tt class="docutils literal"><span class="pre">BOOST_PP_FILENAME_2</span></tt>
  857. instead.</p>
  858. <p>The contents of <tt class="docutils literal"><span class="pre">tiny_size_spec.hpp</span></tt>
  859. should look familiar to you; most of it is the same as <tt class="docutils literal"><span
  860. class="pre">TINY_size</span></tt>'s <em>replacement-list</em>,
  861. without the backslashes:</p>
  862. <pre class="literal-block">#define n BOOST_PP_ITERATION()
  863. template &lt;BOOST_PP_ENUM_PARAMS(n, class T)&gt;
  864. struct tiny_size&lt;
  865. BOOST_PP_ENUM_PARAMS(n,T)
  866. BOOST_PP_COMMA_IF(n)
  867. BOOST_PP_ENUM(BOOST_PP_SUB(TINY_MAX_SIZE,n), TINY_print, none)
  868. &gt;
  869. : mpl::int_&lt;n&gt; {};
  870. #undef n
  871. </pre>
  872. <!-- @import tempfile, os
  873. open(os.path.join(tempfile.gettempdir(),'tiny_size_spec.hpp'), 'w' ).write(str(example))ignore()vertical_options = ['-I'+tempfile.gettempdir(), '-c']
  874. compile('all', options = vertical_options, pop = 1) -->
  875. <p>The Library transmits the iteration index to us in the result
  876. of <tt class="docutils literal"><span class="pre">BOOST_PP_ITERATION()</span></tt>;
  877. <tt class="docutils literal"><span class="pre">n</span></tt> is
  878. nothing more than a convenient local macro used to reduce
  879. syntactic noise. Note that we didn't use <tt class="docutils literal"><span
  880. class="pre">#include</span></tt> guards because we need <tt
  881. class="docutils literal"><span class="pre">tiny_size_spec.hpp</span></tt>
  882. to be processed multiple times.</p>
  883. <p>The preprocessed result should now preserve the line structure
  884. of the pattern and be more verifiable for larger values of <tt
  885. class="docutils literal"><span class="pre">TINY_MAX_SIZE</span></tt>.
  886. For instance, when <tt class="docutils literal"><span class="pre">TINY_MAX_SIZE</span></tt>
  887. is <tt class="docutils literal"><span class="pre">8</span></tt>,
  888. the following excerpt appears in the output of GCC's
  889. preprocessing phase:</p>
  890. <pre class="literal-block"><em>...</em>
  891. template &lt; class T0 , class T1 , class T2 , class T3&gt;
  892. struct tiny_size&lt;
  893. T0 , T1 , T2 , T3
  894. ,
  895. none , none , none , none
  896. &gt;
  897. : mpl::int_&lt;4&gt; {};
  898. template &lt; class T0 , class T1 , class T2 , class T3 , class T4&gt;
  899. struct tiny_size&lt;
  900. T0 , T1 , T2 , T3 , T4
  901. ,
  902. none , none , none
  903. &gt;
  904. : mpl::int_&lt;5&gt; {};
  905. <em>...etc.</em>
  906. </pre>
  907. <!-- @compile('all', options = vertical_options + ['-DTINY_MAX_SIZE=8']) -->
  908. </div>
  909. <div class="section" id="self-iteration">
  910. <h4><a name="self-iteration">Self-Iteration</a></h4>
  911. <p>Creating an entirely new file like <tt class="docutils literal"><span
  912. class="pre">tiny_size_spec.hpp</span></tt> each time we want
  913. to express a trivial code pattern for file repetition can be
  914. inconvenient. Fortunately, the library provides a macro that
  915. allows us to place the pattern right in the file that invokes
  916. the iteration. <tt class="docutils literal"><span class="pre">BOOST_PP_IS_ITERATING</span></tt>
  917. is defined to a nonzero value whenever we're inside an
  918. iteration. We can use that value to select between the part of a
  919. file that invokes the iteration and the part that provides the
  920. repeated pattern. Here's a complete <tt class="docutils literal"><span
  921. class="pre">tiny_size.hpp</span></tt> file that demonstrates
  922. self-iteration. Note in particular the placement and use of the
  923. <tt class="docutils literal"><span class="pre">#include</span></tt>
  924. guard <tt class="docutils literal"><span class="pre">TINY_SIZE_HPP_INCLUDED</span></tt>:</p>
  925. <pre class="literal-block">#ifndef <strong>BOOST_PP_IS_ITERATING</strong>
  926. # ifndef TINY_SIZE_HPP_INCLUDED
  927. # define TINY_SIZE_HPP_INCLUDED
  928. # include &lt;boost/preprocessor/repetition.hpp&gt;
  929. # include &lt;boost/preprocessor/arithmetic/sub.hpp&gt;
  930. # include &lt;boost/preprocessor/punctuation/comma_if.hpp&gt;
  931. # include &lt;boost/preprocessor/iteration/iterate.hpp&gt;
  932. # ifndef TINY_MAX_SIZE
  933. # define TINY_MAX_SIZE 3 // default maximum size is 3
  934. # endif
  935. // primary template
  936. template &lt;BOOST_PP_ENUM_PARAMS(TINY_MAX_SIZE, class T)&gt;
  937. struct tiny_size
  938. : mpl::int_&lt;TINY_MAX_SIZE&gt;
  939. {};
  940. // generate specializations
  941. # define BOOST_PP_ITERATION_LIMITS (0, TINY_MAX_SIZE - 1)
  942. # define BOOST_PP_FILENAME_1 "tiny_size.hpp" // this file
  943. # include BOOST_PP_ITERATE()
  944. # endif // TINY_SIZE_HPP_INCLUDED
  945. #else // <strong>BOOST_PP_IS_ITERATING</strong>
  946. # define n BOOST_PP_ITERATION()
  947. # define TINY_print(z, n, data) data
  948. // specialization pattern
  949. template &lt;BOOST_PP_ENUM_PARAMS(n, class T)&gt;
  950. struct tiny_size&lt;
  951. BOOST_PP_ENUM_PARAMS(n,T)
  952. BOOST_PP_COMMA_IF(n)
  953. BOOST_PP_ENUM(BOOST_PP_SUB(TINY_MAX_SIZE,n), TINY_print, none)
  954. &gt;
  955. : mpl::int_&lt;n&gt; {};
  956. # undef TINY_print
  957. # undef n
  958. #endif // <strong>BOOST_PP_IS_ITERATING</strong>
  959. </pre>
  960. <!-- @compile(source_file = 'tiny_size.hpp') --> </div>
  961. <div class="section" id="more">
  962. <h4><a name="more">More</a></h4>
  963. <p>There's a good deal more to file iteration than what we've been
  964. able to show you here. For more details, we encourage you to
  965. delve into the library's electronic documentation of <tt class="docutils literal"><span
  966. class="pre">BOOST_PP_ITERATE</span></tt> and friends. Also,
  967. it's important to note that no single technique for repetition
  968. is superior to any other: Your choice may depend on convenience,
  969. verifiability, debuggability, compilation speed, and your own
  970. sense of "logical coherence."</p>
  971. </div>
  972. </div>
  973. </div>
  974. <div class="section" id="arithmetic-logical-and-comparison-operations">
  975. <h2><a name="arithmetic-logical-and-comparison-operations">A.4.2   Arithmetic,
  976. Logical, and Comparison Operations</a></h2>
  977. <p>As we mentioned earlier, many of the Preprocessor library
  978. interfaces require single-token numeric arguments, and when those
  979. numbers need to be computed arithmetically, straightforward
  980. arithmetic expressions are inappropriate. We used <tt class="docutils literal"><span
  981. class="pre">BOOST_PP_SUB</span></tt> to subtract two numeric
  982. tokens in our <tt class="docutils literal"><span class="pre">tiny_size</span></tt>
  983. examples. The library contains a suite of operations for
  984. non-negative integral token arithmetic in its <tt class="docutils literal"><span
  985. class="pre">arithmetic/</span></tt> subdirectory, as shown in
  986. Table A.1</p>
  987. <table border="1" class="docutils">
  988. <caption>Preprocessor Library Arithmetic Operations</caption> <colgroup>
  989. <col width="44%" /> <col width="56%" /> </colgroup>
  990. <thead valign="bottom">
  991. <tr>
  992. <th>Expression</th>
  993. <th>Value of Single Token Result</th>
  994. </tr>
  995. </thead>
  996. <tbody valign="top">
  997. <tr>
  998. <td><tt class="docutils literal"><span class="pre">BOOST_PP_ADD(x,y)</span></tt></td>
  999. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1000. class="pre">+</span> <span class="pre">y</span></tt></td>
  1001. </tr>
  1002. <tr>
  1003. <td><tt class="docutils literal"><span class="pre">BOOST_PP_DEC(x)</span></tt></td>
  1004. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1005. class="pre">-</span> <span class="pre">1</span></tt></td>
  1006. </tr>
  1007. <tr>
  1008. <td><tt class="docutils literal"><span class="pre">BOOST_PP_DIV(x,y)</span></tt></td>
  1009. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1010. class="pre">/</span> <span class="pre">y</span></tt></td>
  1011. </tr>
  1012. <tr>
  1013. <td><tt class="docutils literal"><span class="pre">BOOST_PP_INC(x)</span></tt></td>
  1014. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1015. class="pre">+</span> <span class="pre">1</span></tt></td>
  1016. </tr>
  1017. <tr>
  1018. <td><tt class="docutils literal"><span class="pre">BOOST_PP_MOD(x,y)</span></tt></td>
  1019. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1020. class="pre">%</span> <span class="pre">y</span></tt></td>
  1021. </tr>
  1022. <tr>
  1023. <td><tt class="docutils literal"><span class="pre">BOOST_PP_MUL(x,y)</span></tt></td>
  1024. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1025. class="pre">*</span> <span class="pre">y</span></tt></td>
  1026. </tr>
  1027. <tr>
  1028. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SUB(x,y)</span></tt></td>
  1029. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1030. class="pre">-</span> <span class="pre">y</span></tt></td>
  1031. </tr>
  1032. </tbody>
  1033. </table>
  1034. <p>The <tt class="docutils literal"><span class="pre">logical/</span></tt>
  1035. subdirectory contains the convenient Boolean token operations shown
  1036. in Table A.2 and the more efficient operations shown in Table A.3,
  1037. which require that their operands are either <tt class="docutils literal"><span
  1038. class="pre">0</span></tt> or <tt class="docutils literal"><span
  1039. class="pre">1</span></tt> (a single bit).</p>
  1040. <table border="1" class="docutils">
  1041. <caption>Preprocessor Library Integer Logical Operations</caption> <colgroup>
  1042. <col width="44%" /> <col width="56%" /> </colgroup>
  1043. <thead valign="bottom">
  1044. <tr>
  1045. <th>Expression</th>
  1046. <th>Value of Single Token Result</th>
  1047. </tr>
  1048. </thead>
  1049. <tbody valign="top">
  1050. <tr>
  1051. <td><tt class="docutils literal"><span class="pre">BOOST_PP_AND(x,y)</span></tt></td>
  1052. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1053. class="pre">&amp;&amp;</span> <span class="pre">y</span></tt></td>
  1054. </tr>
  1055. <tr>
  1056. <td><tt class="docutils literal"><span class="pre">BOOST_PP_NOR(x,y)</span></tt></td>
  1057. <td><tt class="docutils literal"><span class="pre">!(x</span> <span
  1058. class="pre">||</span> <span class="pre">y)</span></tt></td>
  1059. </tr>
  1060. <tr>
  1061. <td><tt class="docutils literal"><span class="pre">BOOST_PP_OR(x,y)</span></tt></td>
  1062. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1063. class="pre">||</span> <span class="pre">y</span></tt></td>
  1064. </tr>
  1065. <tr>
  1066. <td><tt class="docutils literal"><span class="pre">BOOST_PP_XOR(x,y)</span></tt></td>
  1067. <td><tt class="docutils literal"><span class="pre">(bool)x</span>
  1068. <span class="pre">!=</span> <span class="pre">(bool)y</span>  
  1069. <span class="pre">?</span> <span class="pre">1</span> <span
  1070. class="pre">:</span> <span class="pre">0</span></tt></td>
  1071. </tr>
  1072. <tr>
  1073. <td><tt class="docutils literal"><span class="pre">BOOST_PP_NOT(x)</span></tt></td>
  1074. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1075. class="pre">?</span> <span class="pre">0</span> <span class="pre">:</span>
  1076. <span class="pre">1</span></tt></td>
  1077. </tr>
  1078. <tr>
  1079. <td><tt class="docutils literal"><span class="pre">BOOST_PP_BOOL(x)</span></tt></td>
  1080. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1081. class="pre">?</span> <span class="pre">1</span> <span class="pre">:</span>
  1082. <span class="pre">0</span></tt></td>
  1083. </tr>
  1084. </tbody>
  1085. </table>
  1086. <table border="1" class="docutils">
  1087. <caption>Preprocessor Library Bit Logical Operations</caption> <colgroup>
  1088. <col width="44%" /> <col width="56%" /> </colgroup>
  1089. <thead valign="bottom">
  1090. <tr>
  1091. <th>Expression</th>
  1092. <th>Value of Single Token Result</th>
  1093. </tr>
  1094. </thead>
  1095. <tbody valign="top">
  1096. <tr>
  1097. <td><tt class="docutils literal"><span class="pre">BOOST_PP_BITAND(x,y)</span></tt></td>
  1098. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1099. class="pre">&amp;&amp;</span> <span class="pre">y</span></tt></td>
  1100. </tr>
  1101. <tr>
  1102. <td><tt class="docutils literal"><span class="pre">BOOST_PP_BITNOR(x,y)</span></tt></td>
  1103. <td><tt class="docutils literal"><span class="pre">!(x</span> <span
  1104. class="pre">||</span> <span class="pre">y)</span></tt></td>
  1105. </tr>
  1106. <tr>
  1107. <td><tt class="docutils literal"><span class="pre">BOOST_PP_BITOR(x,y)</span></tt></td>
  1108. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1109. class="pre">||</span> <span class="pre">y</span></tt></td>
  1110. </tr>
  1111. <tr>
  1112. <td><tt class="docutils literal"><span class="pre">BOOST_PP_BITXOR(x,y)</span></tt></td>
  1113. <td><tt class="docutils literal"><span class="pre">(bool)x</span>
  1114. <span class="pre">!=</span> <span class="pre">(bool)y</span>  
  1115. <span class="pre">?</span> <span class="pre">1</span> <span
  1116. class="pre">:</span> <span class="pre">0</span></tt></td>
  1117. </tr>
  1118. <tr>
  1119. <td><tt class="docutils literal"><span class="pre">BOOST_PP_COMPL(x)</span></tt></td>
  1120. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1121. class="pre">?</span> <span class="pre">0</span> <span class="pre">:</span>
  1122. <span class="pre">1</span></tt></td>
  1123. </tr>
  1124. </tbody>
  1125. </table>
  1126. <p>Finally, the <tt class="docutils literal"><span class="pre">comparison/</span></tt>
  1127. subdirectory provides the token integral comparison operations shown
  1128. in Table A.4.</p>
  1129. <table border="1" class="docutils">
  1130. <caption>Preprocessor Library Comparison Operations</caption> <colgroup>
  1131. <col width="46%" /> <col width="54%" /> </colgroup>
  1132. <thead valign="bottom">
  1133. <tr>
  1134. <th>Expression</th>
  1135. <th>Value of Single Token Result</th>
  1136. </tr>
  1137. </thead>
  1138. <tbody valign="top">
  1139. <tr>
  1140. <td><tt class="docutils literal"><span class="pre">BOOST_PP_EQUAL(x,y)</span></tt></td>
  1141. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1142. class="pre">==</span> <span class="pre">y</span>   <span
  1143. class="pre">?</span> <span class="pre">1</span> <span class="pre">:</span>
  1144. <span class="pre">0</span></tt></td>
  1145. </tr>
  1146. <tr>
  1147. <td><tt class="docutils literal"><span class="pre">BOOST_PP_NOT_EQUAL(x,y)</span></tt></td>
  1148. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1149. class="pre">!=</span> <span class="pre">y</span>   <span
  1150. class="pre">?</span> <span class="pre">1</span> <span class="pre">:</span>
  1151. <span class="pre">0</span></tt></td>
  1152. </tr>
  1153. <tr>
  1154. <td><tt class="docutils literal"><span class="pre">BOOST_PP_LESS(x,y)</span></tt></td>
  1155. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1156. class="pre">&lt;</span> <span class="pre">y</span>    <span
  1157. class="pre">?</span> <span class="pre">1</span> <span class="pre">:</span>
  1158. <span class="pre">0</span></tt></td>
  1159. </tr>
  1160. <tr>
  1161. <td><tt class="docutils literal"><span class="pre">BOOST_PP_LESS_EQUAL(x,y)</span></tt></td>
  1162. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1163. class="pre">&lt;=</span> <span class="pre">y</span>   <span
  1164. class="pre">?</span> <span class="pre">1</span> <span class="pre">:</span>
  1165. <span class="pre">0</span></tt></td>
  1166. </tr>
  1167. <tr>
  1168. <td><tt class="docutils literal"><span class="pre">BOOST_PP_GREATER(x,y)</span></tt></td>
  1169. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1170. class="pre">&gt;</span> <span class="pre">y</span>    <span
  1171. class="pre">?</span> <span class="pre">1</span> <span class="pre">:</span>
  1172. <span class="pre">0</span></tt></td>
  1173. </tr>
  1174. <tr>
  1175. <td><tt class="docutils literal"><span class="pre">BOOST_PP_GREATER_EQUAL(x,y)</span></tt></td>
  1176. <td><tt class="docutils literal"><span class="pre">x</span> <span
  1177. class="pre">&gt;=</span> <span class="pre">y</span>   <span
  1178. class="pre">?</span> <span class="pre">1</span> <span class="pre">:</span>
  1179. <span class="pre">0</span></tt></td>
  1180. </tr>
  1181. </tbody>
  1182. </table>
  1183. <p>Because it's common to have a choice among several workable
  1184. comparison operators, it may be useful to know that <tt class="docutils literal"><span
  1185. class="pre">BOOST_PP_EQUAL</span></tt> and <tt class="docutils literal"><span
  1186. class="pre">BOOST_PP_NOT_EQUAL</span></tt> are likely to be O(1)
  1187. while the other comparison operators are generally slower.</p>
  1188. </div>
  1189. <div class="section" id="control-structures">
  1190. <h2><a name="control-structures">A.4.3   Control Structures</a></h2>
  1191. <p>In its <tt class="docutils literal"><span class="pre">control/</span></tt>
  1192. directory, the Preprocessor Library supplies a macro <tt class="docutils literal"><span
  1193. class="pre">BOOST_PP_IF(c,t,f)</span></tt> that fulfills a
  1194. similar role to the one filled by <tt class="docutils literal"><span
  1195. class="pre">mpl::if_</span></tt>. To explore the "control"
  1196. group, we'll generate code for a framework of generic function
  1197. objects: the Boost Function Library.<a class="footnote-reference" href="#function"
  1198. id="id10" name="id10">[8]</a> <tt class="docutils literal"><span
  1199. class="pre">boost::function</span></tt> is partially specialized
  1200. to match function type arguments of each arity up to the maximum
  1201. supported by the library:</p>
  1202. <pre class="literal-block">template &lt;class Signature&gt; struct function; // primary template
  1203. template &lt;class R&gt; // arity = 0
  1204. struct function&lt;R()&gt;
  1205. <em>definition not shown...</em>
  1206. template &lt;class R, class A0&gt; // arity = 1
  1207. struct function&lt;R(A0)&gt;
  1208. <em>definition not shown...</em>
  1209. template &lt;class R, class A0, class A1&gt; // arity = 2
  1210. struct function&lt;R(A0,A1)&gt;
  1211. <em>definition not shown...</em>
  1212. template &lt;class R, class A0, class A1, class A2&gt; // arity = 3
  1213. struct function&lt;R(A0,A1,A2)&gt;
  1214. <em>definition not shown...</em>
  1215. <em>etc.</em>
  1216. </pre>
  1217. <!-- @example.replace(')>', ')>;')
  1218. compile() -->
  1219. <table class="docutils footnote" frame="void" id="function" rules="none">
  1220. <colgroup><col class="label" /><col /></colgroup>
  1221. <tbody valign="top">
  1222. <tr>
  1223. <td class="label"><a class="fn-backref" href="#id10" name="function">[8]</a></td>
  1224. <td>We touched briefly on the design of Boost Function when we
  1225. discussed type erasure in Chapter 9. See the Function library
  1226. documentation at <tt class="docutils literal"><span class="pre">boost_1_32_0/libs/function/index.html</span></tt>
  1227. on the CD that accompanies this book for more information.</td>
  1228. </tr>
  1229. </tbody>
  1230. </table>
  1231. <p>We've already covered a few strategies that can be used to generate
  1232. the pattern above, so we won't belabor that part of the problem; the
  1233. file iteration approach we used for <tt class="docutils literal"><span
  1234. class="pre">tiny_size</span></tt> would be fine:</p>
  1235. <pre class="literal-block">#ifndef BOOST_PP_IS_ITERATING
  1236. # ifndef BOOST_FUNCTION_HPP_INCLUDED
  1237. # define BOOST_FUNCTION_HPP_INCLUDED
  1238. # include &lt;boost/preprocessor/repetition.hpp&gt;
  1239. # include &lt;boost/preprocessor/iteration/iterate.hpp&gt;
  1240. # ifndef FUNCTION_MAX_ARITY
  1241. # define FUNCTION_MAX_ARITY 15
  1242. # endif
  1243. <strong>template &lt;class Signature&gt; struct function;</strong> // primary template
  1244. // generate specializations
  1245. # define BOOST_PP_ITERATION_LIMITS (0, FUNCTION_MAX_ARITY)
  1246. # define BOOST_PP_FILENAME_1 "boost/function.hpp" // this file
  1247. # include BOOST_PP_ITERATE()
  1248. # endif // BOOST_FUNCTION_HPP_INCLUDED
  1249. #else // BOOST_PP_IS_ITERATING
  1250. # define n BOOST_PP_ITERATION()
  1251. // specialization pattern
  1252. <strong>template &lt;class R BOOST_PP_ENUM_TRAILING_PARAMS(n, class A)&gt;</strong>
  1253. <strong>struct function&lt;R ( BOOST_PP_ENUM_PARAMS(n,A) )&gt;</strong>
  1254. <em>definition not shown...</em>
  1255. # undef n
  1256. #endif // BOOST_PP_IS_ITERATING
  1257. </pre>
  1258. <p><tt class="docutils literal"><span class="pre">BOOST_PP_ENUM_TRAILING_PARAMS</span></tt>,
  1259. used above, is just like <tt class="docutils literal"><span class="pre">BOOST_PP_ENUM_PARAMS</span></tt>
  1260. except that when its first argument is not <tt class="docutils literal"><span
  1261. class="pre">0</span></tt>, it generates a leading comma.</p>
  1262. <!-- @example.replace_emphasis(';//')
  1263. tmpdir = tempfile.gettempdir()tmpboost = os.path.join(tmpdir,'boost')try: os.mkdir(tmpboost)except: pass
  1264. tmp_boost_function = os.path.join(tmpdir, 'boost/function.hpp')compile( options = vertical_options , source_file = tmp_boost_function
  1265. , pop = None) -->
  1266. <div class="section" id="argument-selection">
  1267. <h3><a name="argument-selection">A.4.3.1   Argument Selection</a></h3>
  1268. <p>For the sake of interoperability with C++ standard library
  1269. algorithms, it might be nice if <tt class="docutils literal"><span
  1270. class="pre">function</span></tt>s of one or two arguments were
  1271. derived from appropriate specializations of <tt class="docutils literal"><span
  1272. class="pre">std::unary_function</span></tt> or <tt class="docutils literal"><span
  1273. class="pre">std::binary_function</span></tt>, respectively.<a
  1274. class="footnote-reference" href="#ebo" id="id11" name="id11">[9]</a>
  1275. <tt class="docutils literal"><span class="pre">BOOST_PP_IF</span></tt>
  1276. is a great tool for dealing with special cases:</p>
  1277. <pre class="literal-block"># include &lt;boost/preprocessor/control/if.hpp&gt;
  1278. # include &lt;boost/preprocessor/comparison/equal.hpp&gt;
  1279. // specialization pattern
  1280. template &lt;class R BOOST_PP_ENUM_TRAILING_PARAMS(n, class A)&gt;
  1281. struct function&lt;R ( BOOST_PP_ENUM_PARAMS(n,A) )&gt;
  1282. BOOST_PP_IF(
  1283. BOOST_PP_EQUAL(n,2), <strong>: std::binary_function&lt;A0, A1, R&gt;</strong>
  1284. , BOOST_PP_IF(
  1285. BOOST_PP_EQUAL(n,1), <strong>: std::unary_function&lt;A0, R&gt;</strong>
  1286. , <em>...empty argument...</em>
  1287. )
  1288. )
  1289. { <em>...class body omitted...</em> };
  1290. </pre>
  1291. <!-- @pp_failure() -->
  1292. <table class="docutils footnote" frame="void" id="ebo" rules="none">
  1293. <colgroup><col class="label" /><col /></colgroup>
  1294. <tbody valign="top">
  1295. <tr>
  1296. <td class="label"><a class="fn-backref" href="#id11" name="ebo">[9]</a></td>
  1297. <td>While derivation from <tt class="docutils literal"><span
  1298. class="pre">std::unary_function</span></tt> or <tt class="docutils literal"><span
  1299. class="pre">std::binary_function</span></tt> might be
  1300. necessary for interoperability with some older library
  1301. implementations, it may inhibit the Empty Base Optimization
  1302. (EBO) from taking effect when two such derived classes are
  1303. part of the same object. For more information, see section
  1304. 9.4. In general, it's better to expose <tt class="docutils literal"><span
  1305. class="pre">first_argument_type</span></tt>, <tt class="docutils literal"><span
  1306. class="pre">second_argument_type</span></tt>, and <tt class="docutils literal"><span
  1307. class="pre">result_type</span></tt> <tt class="docutils literal"><span
  1308. class="pre">typedef</span></tt>s directly.</td>
  1309. </tr>
  1310. </tbody>
  1311. </table>
  1312. <p>Well, our first attempt has run into several problems. First off,
  1313. you're not allowed to pass an empty argument to the preprocessor.<a
  1314. class="footnote-reference" href="#c99" id="id12" name="id12">[3]</a>
  1315. Secondly, because angle brackets don't get special treatment, the
  1316. commas in the <tt class="docutils literal"><span class="pre">std::unary_function</span></tt>
  1317. and <tt class="docutils literal"><span class="pre">std::binary_function</span></tt>
  1318. specializations above are treated as macro argument separators,
  1319. and the preprocessor will complain that we've passed the wrong
  1320. number of arguments to <tt class="docutils literal"><span class="pre">BOOST_PP_IF</span></tt>
  1321. in two places.</p>
  1322. <p>Because it captures all of the issues, let's focus on the inner <tt
  1323. class="docutils literal"><span class="pre">BOOST_PP_IF</span></tt>
  1324. invocation for a moment. The strategy that <tt class="docutils literal"><span
  1325. class="pre">mpl::eval_if</span></tt> uses, of selecting a
  1326. nullary function to invoke, could work nicely here. The
  1327. preprocessor doesn't have a direct analogue for <tt class="docutils literal"><span
  1328. class="pre">mpl::eval_if</span></tt>, but it doesn't really
  1329. need one: We can get the right effect by adding a second set of
  1330. parentheses to <tt class="docutils literal"><span class="pre">BOOST_PP_IF</span></tt>.</p>
  1331. <pre class="literal-block">#define BOOST_FUNCTION_unary() : std::unary_function&lt;A0,R&gt;
  1332. #define BOOST_FUNCTION_empty() // nothing
  1333. ...
  1334. , BOOST_PP_IF(
  1335. BOOST_PP_EQUAL(n,1), BOOST_FUNCTION_unary
  1336. , BOOST_FUNCTION_empty
  1337. )<strong>()</strong>
  1338. #undef BOOST_FUNCTION_empty
  1339. #undef BOOST_FUNCTION_unary
  1340. </pre>
  1341. <!-- @ignore() -->
  1342. <p>A nullary macro that generates nothing is so commonly needed that
  1343. the library's "facilities" group provides one: <tt class="docutils literal"><span
  1344. class="pre">BOOST_PP_EMPTY</span></tt>. To complete the
  1345. example we'll need to delay evaluation all the way to the outer <tt
  1346. class="docutils literal"><span class="pre">BOOST_PP_IF</span></tt>
  1347. invocation, because <tt class="docutils literal"><span class="pre">std::binary_function&lt;A0,A1,R&gt;</span></tt>
  1348. also has a "comma problem":</p>
  1349. <pre class="literal-block"># include &lt;boost/preprocessor/<strong>facilities/empty.hpp</strong>&gt;
  1350. # define BOOST_FUNCTION_binary() : std::binary_function&lt;A0,A1,R&gt;
  1351. # define BOOST_FUNCTION_unary() : std::unary_function&lt;A0,R&gt;
  1352. // specialization pattern
  1353. template &lt;class R BOOST_PP_ENUM_TRAILING_PARAMS(n, class A)&gt;
  1354. struct function&lt;R ( BOOST_PP_ENUM_PARAMS(n,A) )&gt;
  1355. BOOST_PP_IF(
  1356. BOOST_PP_EQUAL(n,2), BOOST_FUNCTION_binary
  1357. , BOOST_PP_IF(
  1358. BOOST_PP_EQUAL(n,1), BOOST_FUNCTION_unary
  1359. , <strong>BOOST_PP_EMPTY</strong>
  1360. )
  1361. )<strong>()</strong>
  1362. {
  1363. <em>...class body omitted...</em>
  1364. };
  1365. # undef BOOST_FUNCTION_unary
  1366. # undef BOOST_FUNCTION_binary
  1367. # undef n
  1368. </pre>
  1369. <!-- @stack.pop()
  1370. stack[-1].replace('// specialization pattern', '////\n%s\n////' % str(example))compile(source_file = tmp_boost_function, pop = None) -->
  1371. <p>Note that because we happened to be using file iteration, we
  1372. could have also used <tt class="docutils literal"><span class="pre">#if</span></tt>
  1373. on <tt class="docutils literal"><span class="pre">n</span></tt>'s
  1374. value directly:</p>
  1375. <pre class="literal-block"> template &lt;class R BOOST_PP_ENUM_TRAILING_PARAMS(n, class A)&gt;
  1376. struct function&lt;R ( BOOST_PP_ENUM_PARAMS(n,A) )&gt;
  1377. <strong>#if n == 2</strong>
  1378. : std::binary_function&lt;A0, A1, R&gt;
  1379. <strong>#elif n == 1</strong>
  1380. : std::unary_function&lt;A0, R&gt;
  1381. <strong>#endif</strong>
  1382. </pre>
  1383. <!-- @stack.pop()
  1384. stack[-1].sub( r'////.*////', '////\n%s\n////' % str(example), flags = re.DOTALL)compile(source_file = tmp_boost_function, pop = None) -->
  1385. <p><tt class="docutils literal"><span class="pre">BOOST_PP_IF</span></tt>
  1386. has the advantage of enabling us to encapsulate the logic in a
  1387. reusable macro, parameterized on <tt class="docutils literal"><span
  1388. class="pre">n</span></tt>, that is compatible with all
  1389. repetition constructs:</p>
  1390. <pre class="literal-block">#define BOOST_FUNCTION_BASE(n) \
  1391. BOOST_PP_IF(BOOST_PP_EQUAL(n,2), BOOST_FUNCTION_binary \
  1392. , BOOST_PP_IF(BOOST_PP_EQUAL(n,1), BOOST_FUNCTION_unary \
  1393. , BOOST_PP_EMPTY \
  1394. ) \
  1395. )()
  1396. </pre>
  1397. <!-- @compile(options = ['-E']) --> </div>
  1398. <div class="section" id="other-selection-constructs">
  1399. <h3><a name="other-selection-constructs">A.4.3.2   Other Selection
  1400. Constructs</a></h3>
  1401. <p><tt class="docutils literal"><span class="pre">BOOST_PP_IDENTITY</span></tt>,
  1402. also in the "facilities" group, is an interesting cousin of <tt class="docutils literal"><span
  1403. class="pre">BOOST_PP_EMPTY</span></tt>:</p>
  1404. <pre class="literal-block">#define BOOST_PP_IDENTITY(tokens) tokens BOOST_PP_EMPTY
  1405. </pre>
  1406. <!-- @ignore() -->
  1407. <p>You can think of it as creating a nullary macro that returns <tt
  1408. class="docutils literal"><span class="pre">tokens</span></tt>:
  1409. When empty parentheses are appended, the trailing <tt class="docutils literal"><span
  1410. class="pre">BOOST_PP_EMPTY</span></tt> is expanded leaving
  1411. just <tt class="docutils literal"><span class="pre">tokens</span></tt>
  1412. behind. If we had wanted inheritance from <tt class="docutils literal"><span
  1413. class="pre">mpl::empty_base</span></tt> when <tt class="docutils literal"><span
  1414. class="pre">function</span></tt>'s arity is not one or two, we
  1415. could have used <tt class="docutils literal"><span class="pre">BOOST_PP_IDENTITY</span></tt>:</p>
  1416. <pre class="literal-block">// specialization pattern
  1417. template &lt;class R BOOST_PP_ENUM_TRAILING_PARAMS(n, class A)&gt;
  1418. struct function&lt;R ( BOOST_PP_ENUM_PARAMS(n,A) )&gt;
  1419. BOOST_PP_IF(
  1420. BOOST_PP_EQUAL(n,2), BOOST_FUNCTION_binary
  1421. , BOOST_PP_IF(
  1422. BOOST_PP_EQUAL(n,1), BOOST_FUNCTION_unary
  1423. , <strong>BOOST_PP_IDENTITY(: mpl::empty_base)</strong>
  1424. )
  1425. )<strong>()</strong>
  1426. {
  1427. <em>...class body omitted...</em>
  1428. };
  1429. </pre>
  1430. <!-- @stack.pop()
  1431. stack[-1].sub( r'////.*////', '////\n%s\n////' % str(example), flags = re.DOTALL)compile(source_file = tmp_boost_function, pop = None) -->
  1432. <p>It's also worth knowing about <tt class="docutils literal"><span
  1433. class="pre">BOOST_PP_EXPR_IF</span></tt>, which generates its
  1434. second argument or nothing, depending on the Boolean value of its
  1435. first:</p>
  1436. <pre class="literal-block">#define BOOST_PP_EXPR_IF(c,tokens) \
  1437. BOOST_PP_IF(c,BOOST_PP_IDENTITY(tokens),BOOST_PP_EMPTY)()
  1438. </pre>
  1439. <!-- @example.append(
  1440. 'int BOOST_PP_EXPR_IF(1,main) BOOST_PP_EXPR_IF(0,quack) () {}')compile() -->
  1441. <p>So <tt class="docutils literal"><span class="pre">BOOST_PP_EXPR_IF(1,foo)</span></tt>
  1442. expands to <tt class="docutils literal"><span class="pre">foo</span></tt>,
  1443. while <tt class="docutils literal"><span class="pre">BOOST_PP_EXPR_IF(0,foo)</span></tt>
  1444. expands to nothing.</p>
  1445. </div>
  1446. </div>
  1447. <div class="section" id="token-pasting">
  1448. <h2><a name="token-pasting">A.4.4   Token Pasting</a></h2>
  1449. <p>It would be nice if there were a generic way to access the return
  1450. and parameter types of <em>all</em> function objects, rather than
  1451. just the unary and binary ones. A metafunction returning the
  1452. signature as an MPL sequence would do the trick. We could just
  1453. specialize <tt class="docutils literal"><span class="pre">signature</span></tt>
  1454. for each <tt class="docutils literal"><span class="pre">function</span></tt>
  1455. arity:</p>
  1456. <pre class="literal-block">template &lt;class F&gt; struct signature; // primary template
  1457. // partial specializations for boost::function
  1458. template &lt;class R&gt;
  1459. struct signature&lt;function&lt;R()&gt; &gt;
  1460. : mpl::vector1&lt;R&gt; {};
  1461. template &lt;class R, class A0&gt;
  1462. struct signature&lt;function&lt;R(A0)&gt; &gt;
  1463. : mpl::vector2&lt;R,A0&gt; {};
  1464. template &lt;class R, class A0, class A1&gt;
  1465. struct signature&lt;function&lt;R(A0,A1)&gt; &gt;
  1466. : mpl::vector3&lt;R,A0,A1&gt; {};
  1467. ...
  1468. </pre>
  1469. <!-- @example.prepend('template <class T> struct function;')
  1470. compile() -->
  1471. <p>To generate these specializations, we might add the following to
  1472. our pattern:</p>
  1473. <pre class="literal-block">template &lt;class R BOOST_PP_ENUM_TRAILING_PARAMS(n, class A)&gt;
  1474. struct signature&lt;function&lt;R( BOOST_PP_ENUM_PARAMS(n,A) )&gt; &gt;
  1475. : mpl::<strong>BOOST_PP_CAT</strong>(vector,n)&lt;
  1476. R BOOST_PP_ENUM_TRAILING_PARAMS(n,A)
  1477. &gt; {};
  1478. </pre>
  1479. <!-- @stack.pop()
  1480. stack[-1].replace( ';//', ''';// template <class T> struct signature; %s''' % example)
  1481. compile(source_file = tmp_boost_function) -->
  1482. <p><tt class="docutils literal"><span class="pre">BOOST_PP_CAT</span></tt>
  1483. implements <strong>token pasting</strong>; its two arguments are
  1484. "glued" together into a single token. Since this is a
  1485. general-purpose macro, it sits in <tt class="docutils literal"><span
  1486. class="pre">cat.hpp</span></tt> at the top level of the
  1487. library's directory tree.</p>
  1488. <p>Although the preprocessor has a built-in token-pasting operator, <tt
  1489. class="docutils literal"><span class="pre">##</span></tt>, it only
  1490. works within a macro definition. If we'd used it here, it wouldn't
  1491. have taken effect at all:</p>
  1492. <pre class="literal-block">template &lt;class R&gt;
  1493. struct signature&lt;function&lt;R()&gt; &gt;
  1494. : mpl::<strong>vector##1</strong>&lt;R&gt; {};
  1495. template &lt;class R, class A0&gt;
  1496. struct signature&lt;function&lt;R(A0)&gt; &gt;
  1497. : mpl::<strong>vector##2</strong>&lt;R,A0&gt; {};
  1498. template &lt;class R, class A0, class A1&gt;
  1499. struct signature&lt;function&lt;R(A0,A1)&gt; &gt;
  1500. : mpl::<strong>vector##3</strong>&lt;R,A0,A1&gt; {};
  1501. ...
  1502. </pre>
  1503. <!-- @example.replace('##','')
  1504. example.prepend(''' template <class T> struct function; template <class T> struct signature;''')
  1505. compile() -->
  1506. <p>Also, <tt class="docutils literal"><span class="pre">##</span></tt>
  1507. often yields surprising results by taking effect before its
  1508. arguments have been expanded:</p>
  1509. <pre class="literal-block">#define N 10
  1510. #define VEC(i) vector##i
  1511. VEC(N) // vectorN
  1512. </pre>
  1513. <!-- @example.wrap('typedef int vectorN;', 'x;')
  1514. compile() -->
  1515. <p>By contrast, <tt class="docutils literal"><span class="pre">BOOST_PP_CAT</span></tt>
  1516. delays concatenation until after its arguments have been fully
  1517. evaluated:</p>
  1518. <pre class="literal-block">#define N 10
  1519. #define VEC(i) BOOST_PP_CAT(vector,i)
  1520. VEC(N) // vector10
  1521. </pre>
  1522. <!-- @example.wrap('''
  1523. #include <boost/preprocessor/cat.hpp> typedef int vector10; ''', 'x;')compile() -->
  1524. </div>
  1525. <div class="section" id="data-types">
  1526. <h2><a name="data-types">A.4.5   Data Types</a></h2>
  1527. <p>The Preprocessor library also provides <strong>data types</strong>,
  1528. which you can think of as being analogous to the MPL's type
  1529. sequences. Preprocessor data types store <em>macro arguments</em>
  1530. instead of C++ types.</p>
  1531. <div class="section" id="sequences">
  1532. <h3><a name="sequences">A.4.5.1   Sequences</a></h3>
  1533. <p>A <strong>sequence</strong> (or <strong>seq</strong> for short)
  1534. is any string of nonempty parenthesized <em>macro arguments</em>.
  1535. For instance, here's a three-element sequence:</p>
  1536. <pre class="literal-block">#define MY_SEQ (f(12))(a + 1)(foo)
  1537. </pre>
  1538. <!-- @ignore() -->
  1539. <p>Here's how we might use a sequence to generate specializations of
  1540. the <tt class="docutils literal"><span class="pre">is_integral</span></tt>
  1541. template from the Boost Type Traits library (see Chapter 2):</p>
  1542. <pre class="literal-block">#include &lt;boost/preprocessor/seq.hpp&gt;
  1543. template &lt;class T&gt;
  1544. struct is_integral : mpl::false_ {};
  1545. // a seq of integral types with unsigned counterparts
  1546. #define BOOST_TT_basic_ints (char)(short)(int)(long)
  1547. // generate a seq containing "signed t" and "unsigned t"
  1548. #define BOOST_TT_int_pair(r,data,t) (signed t)(unsigned t)
  1549. // a seq of all the integral types
  1550. #define BOOST_TT_ints \
  1551. (bool)(char) \
  1552. BOOST_PP_SEQ_FOR_EACH(BOOST_TT_int_pair, ~, BOOST_TT_basic_ints)
  1553. // generate an is_integral specialization for type t
  1554. #define BOOST_TT_is_integral_spec(r,data,t) \
  1555. template &lt;&gt; \
  1556. struct is_integral&lt;t&gt; : mpl::true_ {};
  1557. BOOST_PP_SEQ_FOR_EACH(BOOST_TT_is_integral_spec, ~, BOOST_TT_ints)
  1558. #undef BOOST_TT_is_integral_spec
  1559. #undef BOOST_TT_ints
  1560. #undef BOOST_TT_int_pair
  1561. #undef BOOST_TT_basic_ints
  1562. </pre>
  1563. <!-- @compile() -->
  1564. <p><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_FOR_EACH</span></tt>
  1565. is a higher-order macro, similar to <tt class="docutils literal"><span
  1566. class="pre">BOOST_PP_REPEAT</span></tt>, that invokes its
  1567. first argument on each element of its third argument.</p>
  1568. <p>Sequences are the most efficient, most flexible, and
  1569. easiest-to-use of the library's data structures, provided that you
  1570. never need to make an empty one: An empty sequence would contain
  1571. no tokens, and so couldn't be passed as a macro argument. The
  1572. other data structures covered here all have an empty
  1573. representation.</p>
  1574. <p>The facilities for manipulating sequences are all in the
  1575. library's <tt class="docutils literal"><span class="pre">seq/</span></tt>
  1576. subdirectory. They are summarized in Table A.5, where <tt class="docutils literal"><span
  1577. class="pre">t</span></tt> is the sequence <tt class="docutils literal"><span
  1578. class="pre">(</span></tt><em>t</em><sub>0</sub><tt class="docutils literal"><span
  1579. class="pre">)(</span></tt><em>t</em><sub>1</sub><tt class="docutils literal"><span
  1580. class="pre">)...(</span></tt><em>t</em><sub>k</sub><tt class="docutils literal"><span
  1581. class="pre">)</span></tt>. Where <em>s</em>, <em>r</em>, and
  1582. <em>d</em> appear, they have a similar purpose to the <tt class="docutils literal"><span
  1583. class="pre">z</span></tt> parameters we discussed earlier (and
  1584. suggested you ignore for now).</p>
  1585. <table border="1" class="docutils">
  1586. <caption>Preprocessor Sequence Operations</caption> <colgroup> <col
  1587. width="51%" /> <col width="49%" /> </colgroup>
  1588. <thead valign="bottom">
  1589. <tr>
  1590. <th>Expression</th>
  1591. <th>Result</th>
  1592. </tr>
  1593. </thead>
  1594. <tbody valign="top">
  1595. <tr>
  1596. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_CAT(t)</span></tt></td>
  1597. <td><em>t</em><sub>0</sub><em>t</em><sub>1</sub>...<em>t</em><sub>k</sub></td>
  1598. </tr>
  1599. <tr>
  1600. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_ELEM(n,t)</span></tt></td>
  1601. <td><em>t</em><sub>n</sub></td>
  1602. </tr>
  1603. <tr>
  1604. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_ENUM(t)</span></tt></td>
  1605. <td><em>t</em><sub>0</sub>, <em>t</em><sub>1</sub>, ...<em>t</em><sub>k</sub></td>
  1606. </tr>
  1607. <tr>
  1608. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_FILTER(pred,data,t)</span></tt></td>
  1609. <td><tt class="docutils literal"><span class="pre">t</span></tt>
  1610. without the elements that don't satisfy <tt class="docutils literal"><span
  1611. class="pre">pred</span></tt></td>
  1612. </tr>
  1613. <tr>
  1614. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_FIRST_N(n,t)</span></tt></td>
  1615. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>0</sub><tt
  1616. class="docutils literal"><span class="pre">)(</span></tt><em>t</em><sub>1</sub><tt
  1617. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1618. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>n-1</sub><tt
  1619. class="docutils literal"><span class="pre">)</span></tt></td>
  1620. </tr>
  1621. <tr>
  1622. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_FOLD_LEFT(op,</span>
  1623. <span class="pre">x,</span> <span class="pre">t)</span></tt></td>
  1624. <td>...<tt class="docutils literal"><span class="pre">op(</span></tt><em>s</em><tt
  1625. class="docutils literal"><span class="pre">,op(</span></tt><em>s</em><tt
  1626. class="docutils literal"><span class="pre">,op(</span></tt><em>s</em><tt
  1627. class="docutils literal"><span class="pre">,x</span></tt>,<em>t</em><sub>0</sub><tt
  1628. class="docutils literal"><span class="pre">),</span></tt><em>t</em><sub>1</sub><tt
  1629. class="docutils literal"><span class="pre">),</span></tt><em>t</em><sub>2</sub><tt
  1630. class="docutils literal"><span class="pre">)</span></tt>...</td>
  1631. </tr>
  1632. <tr>
  1633. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_FOLD_RIGHT(op,</span>
  1634. <span class="pre">x,</span> <span class="pre">t)</span></tt></td>
  1635. <td>...<tt class="docutils literal"><span class="pre">op(</span></tt><em>s</em><tt
  1636. class="docutils literal"><span class="pre">,op(</span></tt><em>s</em><tt
  1637. class="docutils literal"><span class="pre">,op(</span></tt><em>s</em><tt
  1638. class="docutils literal"><span class="pre">,x</span></tt>,<em>t</em><sub>k</sub><tt
  1639. class="docutils literal"><span class="pre">),</span></tt><em>t</em><sub>k-1</sub><tt
  1640. class="docutils literal"><span class="pre">),</span></tt>
  1641. <em>t</em><sub>k-2</sub><tt class="docutils literal"><span class="pre">)</span></tt>...</td>
  1642. </tr>
  1643. <tr>
  1644. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_FOR_EACH(f,</span>
  1645. <span class="pre">x,</span> <span class="pre">t)</span></tt></td>
  1646. <td><tt class="docutils literal"><span class="pre">f(</span></tt><em>r</em><tt
  1647. class="docutils literal"><span class="pre">,</span> <span
  1648. class="pre">x,</span></tt><em>t</em><sub>0</sub><tt class="docutils literal"><span
  1649. class="pre">)</span> <span class="pre">f(</span></tt><em>r</em><tt
  1650. class="docutils literal"><span class="pre">,</span> <span
  1651. class="pre">x,</span></tt><em>t</em><sub>1</sub><tt class="docutils literal"><span
  1652. class="pre">)</span></tt>...<tt class="docutils literal"><span
  1653. class="pre">f(</span></tt><em>r</em><tt class="docutils literal"><span
  1654. class="pre">,</span> <span class="pre">x,</span></tt><em>t</em><sub>k</sub><tt
  1655. class="docutils literal"><span class="pre">)</span></tt></td>
  1656. </tr>
  1657. <tr>
  1658. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_FOR_EACH_I(g,</span>
  1659. <span class="pre">x,</span> <span class="pre">t)</span></tt></td>
  1660. <td><tt class="docutils literal"><span class="pre">g(</span></tt><em>r</em><tt
  1661. class="docutils literal"><span class="pre">,</span> <span
  1662. class="pre">x,</span> <span class="pre">0,</span></tt>
  1663. <em>t</em><sub>0</sub><tt class="docutils literal"><span class="pre">)</span>
  1664. <span class="pre">g(</span></tt><em>r</em><tt class="docutils literal"><span
  1665. class="pre">,</span> <span class="pre">x,</span> <span
  1666. class="pre">1,</span></tt> <em>t</em><sub>1</sub><tt class="docutils literal"><span
  1667. class="pre">)</span></tt>... <tt class="docutils literal"><span
  1668. class="pre">g(</span></tt><em>r</em><tt class="docutils literal"><span
  1669. class="pre">,</span> <span class="pre">x,</span> <span
  1670. class="pre">k,</span></tt> <em>t</em><sub>k</sub><tt class="docutils literal"><span
  1671. class="pre">)</span></tt></td>
  1672. </tr>
  1673. <tr>
  1674. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_FOR_EACH_PRODUCT(h,</span>
  1675. <span class="pre">x,</span> <span class="pre">t)</span></tt></td>
  1676. <td>
  1677. <dl class="first last docutils">
  1678. <dt>Cartesian product—</dt>
  1679. <dd>see online docs</dd>
  1680. </dl>
  1681. </td>
  1682. </tr>
  1683. <tr>
  1684. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_INSERT(t,i,tokens)</span></tt></td>
  1685. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>0</sub><tt
  1686. class="docutils literal"><span class="pre">)(</span></tt><em>t</em><sub>1</sub><tt
  1687. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1688. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>i-1</sub><tt
  1689. class="docutils literal"><span class="pre">)(tokens)</span>
  1690. <span class="pre">(</span></tt><em>t</em><sub>i</sub><tt class="docutils literal"><span
  1691. class="pre">)(</span></tt><em>t</em><sub>i+1</sub><tt class="docutils literal"><span
  1692. class="pre">)</span></tt>...<tt class="docutils literal"><span
  1693. class="pre">(</span></tt><em>t</em><sub>k</sub><tt class="docutils literal"><span
  1694. class="pre">)</span></tt></td>
  1695. </tr>
  1696. <tr>
  1697. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_POP_BACK(t)</span></tt></td>
  1698. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>0</sub><tt
  1699. class="docutils literal"><span class="pre">)(</span></tt><em>t</em><sub>1</sub><tt
  1700. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1701. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>k-1</sub><tt
  1702. class="docutils literal"><span class="pre">)</span></tt></td>
  1703. </tr>
  1704. <tr>
  1705. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_POP_FRONT(t)</span></tt></td>
  1706. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>1</sub><tt
  1707. class="docutils literal"><span class="pre">)(</span></tt><em>t</em><sub>2</sub><tt
  1708. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1709. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>k</sub><tt
  1710. class="docutils literal"><span class="pre">)</span></tt></td>
  1711. </tr>
  1712. <tr>
  1713. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_PUSH_BACK(t,tokens)</span></tt></td>
  1714. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>0</sub><tt
  1715. class="docutils literal"><span class="pre">)(</span></tt><em>t</em><sub>1</sub><tt
  1716. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1717. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>k</sub><tt
  1718. class="docutils literal"><span class="pre">)(tokens)</span></tt></td>
  1719. </tr>
  1720. <tr>
  1721. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_PUSH_FRONT(t,tokens)</span></tt></td>
  1722. <td><tt class="docutils literal"><span class="pre">(tokens)(</span></tt><em>t</em><sub>0</sub><tt
  1723. class="docutils literal"><span class="pre">)(</span></tt><em>t</em><sub>1</sub><tt
  1724. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1725. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>k</sub><tt
  1726. class="docutils literal"><span class="pre">)</span></tt></td>
  1727. </tr>
  1728. <tr>
  1729. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_REMOVE(t,i)</span></tt></td>
  1730. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>0</sub><tt
  1731. class="docutils literal"><span class="pre">)(</span></tt><em>t</em><sub>1</sub><tt
  1732. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1733. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>i-1</sub><tt
  1734. class="docutils literal"><span class="pre">)(</span></tt><em>t</em><sub>i+1</sub><tt
  1735. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1736. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>k</sub><tt
  1737. class="docutils literal"><span class="pre">)</span></tt></td>
  1738. </tr>
  1739. <tr>
  1740. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_REPLACE(t,i,tokens)</span></tt></td>
  1741. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>0</sub><tt
  1742. class="docutils literal"><span class="pre">)(</span></tt><em>t</em><sub>1</sub><tt
  1743. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1744. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>i-1</sub><tt
  1745. class="docutils literal"><span class="pre">)(tokens)(</span></tt><em>t</em><sub>i+1</sub><tt
  1746. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1747. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>k</sub><tt
  1748. class="docutils literal"><span class="pre">)</span></tt></td>
  1749. </tr>
  1750. <tr>
  1751. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_REST_N(n,t)</span></tt></td>
  1752. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>n</sub><tt
  1753. class="docutils literal"><span class="pre">)(</span></tt><em>t</em><sub>n+1</sub><tt
  1754. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1755. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>k</sub><tt
  1756. class="docutils literal"><span class="pre">)</span></tt></td>
  1757. </tr>
  1758. <tr>
  1759. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_REVERSE(t)</span></tt></td>
  1760. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>k</sub><tt
  1761. class="docutils literal"><span class="pre">)(</span></tt><em>t</em><sub>k-1</sub><tt
  1762. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1763. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>0</sub><tt
  1764. class="docutils literal"><span class="pre">)</span></tt></td>
  1765. </tr>
  1766. <tr>
  1767. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_HEAD(t)</span></tt></td>
  1768. <td><em>t</em><sub>0</sub></td>
  1769. </tr>
  1770. <tr>
  1771. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_TAIL(t)</span></tt></td>
  1772. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>1</sub><tt
  1773. class="docutils literal"><span class="pre">)(</span></tt><em>t</em><sub>2</sub><tt
  1774. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1775. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>k</sub><tt
  1776. class="docutils literal"><span class="pre">)</span></tt></td>
  1777. </tr>
  1778. <tr>
  1779. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_SIZE(t)</span></tt></td>
  1780. <td><em>k+1</em></td>
  1781. </tr>
  1782. <tr>
  1783. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_SUBSEQ(t,i,m)</span></tt></td>
  1784. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>i</sub><tt
  1785. class="docutils literal"><span class="pre">)(</span></tt><em>t</em><sub>i+1</sub><tt
  1786. class="docutils literal"><span class="pre">)</span></tt>...<tt
  1787. class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>i+m-1</sub><tt
  1788. class="docutils literal"><span class="pre">)</span></tt></td>
  1789. </tr>
  1790. <tr>
  1791. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_TO_ARRAY(t)</span></tt></td>
  1792. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>k+1</em>
  1793. <tt class="docutils literal"><span class="pre">,(</span></tt><em>t</em><sub>0</sub><tt
  1794. class="docutils literal"><span class="pre">,</span></tt><em>t</em><sub>1</sub><tt
  1795. class="docutils literal"><span class="pre">,</span></tt>...<em>t</em><sub>k</sub><tt
  1796. class="docutils literal"><span class="pre">))</span></tt></td>
  1797. </tr>
  1798. <tr>
  1799. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_TO_TUPLE(t)</span></tt></td>
  1800. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>t</em><sub>0</sub><tt
  1801. class="docutils literal"><span class="pre">,</span></tt> <em>t</em><sub>1</sub><tt
  1802. class="docutils literal"><span class="pre">,</span></tt>...<em>t</em><sub>k</sub><tt
  1803. class="docutils literal"><span class="pre">)</span></tt></td>
  1804. </tr>
  1805. <tr>
  1806. <td><tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_TRANSFORM(f,</span>
  1807. <span class="pre">x,</span> <span class="pre">t)</span></tt></td>
  1808. <td><tt class="docutils literal"><span class="pre">(f(</span></tt><em>r</em><tt
  1809. class="docutils literal"><span class="pre">,x,</span></tt><em>t</em><sub>0</sub><tt
  1810. class="docutils literal"><span class="pre">))</span> <span
  1811. class="pre">(f(</span></tt><em>r</em><tt class="docutils literal"><span
  1812. class="pre">,x,</span></tt><em>t</em><sub>1</sub><tt class="docutils literal"><span
  1813. class="pre">))</span></tt>...<tt class="docutils literal"><span
  1814. class="pre">(f(</span></tt><em>r</em><tt class="docutils literal"><span
  1815. class="pre">,x,</span></tt><em>t</em><sub>k</sub><tt class="docutils literal"><span
  1816. class="pre">))</span></tt></td>
  1817. </tr>
  1818. </tbody>
  1819. </table>
  1820. <p>It's worth noting that while there is no upper limit on the
  1821. length of a sequence, operations such as <tt class="docutils literal"><span
  1822. class="pre">BOOST_PP_SEQ_ELEM</span></tt> that take numeric
  1823. arguments will only work with values up to 256.</p>
  1824. </div>
  1825. <div class="section" id="tuples">
  1826. <h3><a name="tuples">A.4.5.2   Tuples</a></h3>
  1827. <p>A <strong>tuple</strong> is a very simple data structure for
  1828. which the library provides random access and a few other basic
  1829. operations. A tuple takes the form of a parenthesized,
  1830. comma-separated list of <em>macro arguments</em>. For example,
  1831. this is a three-element tuple:</p>
  1832. <pre class="literal-block">#define TUPLE3 (f(12), a + 1, foo)
  1833. </pre>
  1834. <p>The operations in the library's <tt class="docutils literal"><span
  1835. class="pre">tuple/</span></tt> subdirectory can handle tuples
  1836. of up to 25 elements. For example, a tuple's <tt class="docutils literal"><span
  1837. class="pre">N</span></tt>th element can be accessed via <tt class="docutils literal"><span
  1838. class="pre">BOOST_PP_TUPLE_ELEM</span></tt>, as follows:</p>
  1839. <pre class="literal-block"> // length index tuple
  1840. BOOST_PP_TUPLE_ELEM( 3 , 1 , TUPLE3) // a + 1
  1841. </pre>
  1842. <!-- @def gen_id(id = 'a', hdr = 'tuple'):
  1843. example.wrap(''' #include <boost/preprocessor/%s.hpp> int const %s = 0; int const x =''' % (hdr,id), ';')
  1844. compile('all', pop = 1)gen_id() -->
  1845. <p>Notice that we had to pass the tuple's length as the second
  1846. argument to <tt class="docutils literal"><span class="pre">BOOST_PP_TUPLE_ELEM</span></tt>;
  1847. in fact, <em>all</em> tuple operations require explicit
  1848. specification of the tuple's length. We're not going to summarize
  1849. the other four operations in the "tuple" group here—you can
  1850. consult the Preprocessor library's electronic documentation for
  1851. more details. We note, however, that sequences can be transformed
  1852. into tuples with <tt class="docutils literal"><span class="pre">BOOST_PP_SEQ_TO_TUPLE</span></tt>,
  1853. and nonempty tuples can be transformed back into sequences with <tt
  1854. class="docutils literal"><span class="pre">BOOST_PP_TUPLE_TO_SEQ</span></tt>.</p>
  1855. <p>The greatest strength of tuples is that they conveniently take
  1856. the same representation as a macro argument list:</p>
  1857. <pre class="literal-block">#define FIRST_OF_THREE(a1,a2,a3) a1
  1858. #define SECOND_OF_THREE(a1,a2,a3) a2
  1859. #define THIRD_OF_THREE(a1,a2,a3) a3
  1860. // uses tuple as an argument list
  1861. # define SELECT(selector, tuple) <strong>selector tuple</strong>
  1862. SELECT(THIRD_OF_THREE, TUPLE3) // foo
  1863. </pre>
  1864. <!-- @gen_id('foo') --> </div>
  1865. <div class="section" id="arrays">
  1866. <h3><a name="arrays">A.4.5.3   Arrays</a></h3>
  1867. <p>An <strong>array</strong> is just a tuple containing a
  1868. non-negative integer and a tuple of that length:</p>
  1869. <pre class="literal-block">#define ARRAY3 ( 3, TUPLE3 )
  1870. </pre>
  1871. <p>Because an array carries its length around with it, the library's
  1872. interface for operating on arrays is much more convenient than the
  1873. one used for tuples:</p>
  1874. <pre class="literal-block">BOOST_PP_ARRAY_ELEM(1, ARRAY3) // a + 1
  1875. </pre>
  1876. <!-- @gen_id(hdr = 'array')
  1877. del stack[-2:] -->
  1878. <p>The facilities for manipulating arrays of up to 25 elements are
  1879. all in the library's <tt class="docutils literal"><span class="pre">array/</span></tt>
  1880. subdirectory. They are summarized in Table A.6, where <tt class="docutils literal"><span
  1881. class="pre">a</span></tt> is the array <tt class="docutils literal"><span
  1882. class="pre">(</span></tt><em>k</em><tt class="docutils literal"><span
  1883. class="pre">,</span> <span class="pre">(</span></tt><em>a</em><sub>0</sub><tt
  1884. class="docutils literal"><span class="pre">,</span></tt><em>a</em><sub>1</sub><tt
  1885. class="docutils literal"><span class="pre">,...</span></tt><em>a</em><sub>k-1</sub><tt
  1886. class="docutils literal"><span class="pre">))</span></tt>.</p>
  1887. <table border="1" class="docutils">
  1888. <caption>Preprocessor Array Operations</caption> <colgroup> <col
  1889. width="52%" /> <col width="48%" /> </colgroup>
  1890. <thead valign="bottom">
  1891. <tr>
  1892. <th>Expression</th>
  1893. <th>Result</th>
  1894. </tr>
  1895. </thead>
  1896. <tbody valign="top">
  1897. <tr>
  1898. <td><tt class="docutils literal"><span class="pre">BOOST_PP_ARRAY_DATA(a)</span></tt></td>
  1899. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>a</em><sub>0</sub><tt
  1900. class="docutils literal"><span class="pre">,</span></tt><em>a</em><sub>1</sub><tt
  1901. class="docutils literal"><span class="pre">,</span></tt>...
  1902. <em>a</em><sub>k-1</sub><tt class="docutils literal"><span class="pre">)</span></tt></td>
  1903. </tr>
  1904. <tr>
  1905. <td><tt class="docutils literal"><span class="pre">BOOST_PP_ARRAY_ELEM(i,a)</span></tt></td>
  1906. <td><em>a</em><sub>i</sub></td>
  1907. </tr>
  1908. <tr>
  1909. <td><tt class="docutils literal"><span class="pre">BOOST_PP_ARRAY_INSERT(a,</span>
  1910. <span class="pre">i,</span> <span class="pre">tokens)</span></tt></td>
  1911. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>k+1</em><tt
  1912. class="docutils literal"><span class="pre">,(</span></tt><em>a</em><sub>0</sub><tt
  1913. class="docutils literal"><span class="pre">,</span></tt><em>a</em><sub>1</sub><tt
  1914. class="docutils literal"><span class="pre">,</span></tt>...<em>a</em><sub>i-1</sub><tt
  1915. class="docutils literal"><span class="pre">,</span> <span
  1916. class="pre">tokens,</span></tt> <em>a</em><sub>i</sub><tt
  1917. class="docutils literal"><span class="pre">,</span></tt><em>a</em><sub>i+1</sub><tt
  1918. class="docutils literal"><span class="pre">,</span></tt>...
  1919. <em>a</em><sub>k-1</sub><tt class="docutils literal"><span class="pre">))</span></tt></td>
  1920. </tr>
  1921. <tr>
  1922. <td><tt class="docutils literal"><span class="pre">BOOST_PP_ARRAY_POP_BACK(a)</span></tt></td>
  1923. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>k-1</em><tt
  1924. class="docutils literal"><span class="pre">,(</span></tt><em>a</em><sub>0</sub><tt
  1925. class="docutils literal"><span class="pre">,</span></tt><em>a</em><sub>1</sub><tt
  1926. class="docutils literal"><span class="pre">,</span></tt>...
  1927. <em>a</em><sub>k-2</sub><tt class="docutils literal"><span class="pre">))</span></tt></td>
  1928. </tr>
  1929. <tr>
  1930. <td><tt class="docutils literal"><span class="pre">BOOST_PP_ARRAY_POP_FRONT(a)</span></tt></td>
  1931. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>k-1</em><tt
  1932. class="docutils literal"><span class="pre">,(</span></tt><em>a</em><sub>1</sub><tt
  1933. class="docutils literal"><span class="pre">,</span></tt><em>a</em><sub>2</sub><tt
  1934. class="docutils literal"><span class="pre">,</span></tt>...
  1935. <em>a</em><sub>k-1</sub><tt class="docutils literal"><span class="pre">))</span></tt></td>
  1936. </tr>
  1937. <tr>
  1938. <td><tt class="docutils literal"><span class="pre">BOOST_PP_ARRAY_PUSH_BACK(a,</span>
  1939. <span class="pre">tokens)</span></tt></td>
  1940. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>k+1</em><tt
  1941. class="docutils literal"><span class="pre">,(</span></tt><em>a</em><sub>0</sub><tt
  1942. class="docutils literal"><span class="pre">,</span></tt><em>a</em><sub>1</sub><tt
  1943. class="docutils literal"><span class="pre">,</span></tt>...
  1944. <em>a</em><sub>k-1</sub><tt class="docutils literal"><span class="pre">,</span>
  1945. <span class="pre">tokens))</span></tt></td>
  1946. </tr>
  1947. <tr>
  1948. <td><tt class="docutils literal"><span class="pre">BOOST_PP_ARRAY_PUSH_FRONT(a,</span>
  1949. <span class="pre">tokens)</span></tt></td>
  1950. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>k+1</em><tt
  1951. class="docutils literal"><span class="pre">,(tokens,</span></tt>
  1952. <em>a</em><sub>1</sub><tt class="docutils literal"><span class="pre">,</span></tt><em>a</em><sub>2</sub><tt
  1953. class="docutils literal"><span class="pre">,</span></tt>...
  1954. <em>a</em><sub>k-1</sub><tt class="docutils literal"><span class="pre">))</span></tt></td>
  1955. </tr>
  1956. <tr>
  1957. <td><tt class="docutils literal"><span class="pre">BOOST_PP_ARRAY_REMOVE(a,</span>
  1958. <span class="pre">i)</span></tt></td>
  1959. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>k-1</em><tt
  1960. class="docutils literal"><span class="pre">,(</span></tt><em>a</em><sub>0</sub><tt
  1961. class="docutils literal"><span class="pre">,</span></tt><em>a</em><sub>1</sub><tt
  1962. class="docutils literal"><span class="pre">,</span></tt>...
  1963. <em>a</em><sub>i-1</sub><tt class="docutils literal"><span class="pre">,</span></tt><em>a</em><sub>i+1</sub><tt
  1964. class="docutils literal"><span class="pre">,</span></tt>...
  1965. <em>a</em><sub>k-1</sub><tt class="docutils literal"><span class="pre">))</span></tt></td>
  1966. </tr>
  1967. <tr>
  1968. <td><tt class="docutils literal"><span class="pre">BOOST_PP_ARRAY_REPLACE(a,</span>
  1969. <span class="pre">i,</span> <span class="pre">tokens)</span></tt></td>
  1970. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>k</em><tt
  1971. class="docutils literal"><span class="pre">,(</span></tt><em>a</em><sub>0</sub><tt
  1972. class="docutils literal"><span class="pre">,</span></tt><em>a</em><sub>1</sub><tt
  1973. class="docutils literal"><span class="pre">,</span></tt>...
  1974. <em>a</em><sub>i-1</sub><tt class="docutils literal"><span class="pre">,</span>
  1975. <span class="pre">tokens,</span></tt> <em>a</em><sub>i+1</sub><tt
  1976. class="docutils literal"><span class="pre">,</span></tt>...
  1977. <em>a</em><sub>k-1</sub><tt class="docutils literal"><span class="pre">))</span></tt></td>
  1978. </tr>
  1979. <tr>
  1980. <td><tt class="docutils literal"><span class="pre">BOOST_PP_ARRAY_REVERSE(a)</span></tt></td>
  1981. <td><tt class="docutils literal"><span class="pre">(</span></tt><em>k</em><tt
  1982. class="docutils literal"><span class="pre">,(</span></tt><em>a</em><sub>k-1</sub><tt
  1983. class="docutils literal"><span class="pre">,</span></tt><em>a</em><sub>k-2</sub><tt
  1984. class="docutils literal"><span class="pre">,</span></tt>...
  1985. <em>a</em><sub>1</sub><tt class="docutils literal"><span class="pre">,</span></tt><em>a</em><sub>0</sub><tt
  1986. class="docutils literal"><span class="pre">))</span></tt></td>
  1987. </tr>
  1988. <tr>
  1989. <td><tt class="docutils literal"><span class="pre">BOOST_PP_ARRAY_SIZE(a)</span></tt></td>
  1990. <td><em>k</em></td>
  1991. </tr>
  1992. </tbody>
  1993. </table>
  1994. </div>
  1995. <div class="section" id="lists">
  1996. <h3><a name="lists">A.4.5.4   Lists</a></h3>
  1997. <p>A <strong>list</strong> is a two-element tuple whose first
  1998. element is the first element of the list, and whose second element
  1999. is a list of the remaining elements, or <tt class="docutils literal"><span
  2000. class="pre">BOOST_PP_NIL</span></tt> if there are no remaining
  2001. elements. Lists have access characteristics similar to those of a
  2002. runtime linked list. Here is a three-element list:</p>
  2003. <pre class="literal-block">#define LIST3 (<strong>f(12)</strong>, (<strong>a + 1</strong>, (<strong>foo</strong>, BOOST_PP_NIL)))
  2004. </pre>
  2005. <!-- @ignore() -->
  2006. <p>The facilities for manipulating lists are all in the library's <tt
  2007. class="docutils literal"><span class="pre">list/</span></tt>
  2008. subdirectory. Because the operations are a subset of those
  2009. provided for sequences, we're not going to summarize them here—it
  2010. should be easy to understand the list operations by reading the
  2011. documentation on the basis of our coverage of sequences.</p>
  2012. <p>Like sequences, lists have no fixed upper length bound. Unlike
  2013. sequences, lists can also be empty. It's rare to need more than 25
  2014. elements in a preprocessor data structure, and lists tend to be
  2015. slower to manipulate and harder to read than any of the other
  2016. structures, so they should normally be used only as a last resort.</p>
  2017. </div>
  2018. </div>
  2019. </div>
  2020. <div class="section" id="exercise">
  2021. <h1><a name="exercise">A.5   Exercise</a></h1>
  2022. <dl class="docutils">
  2023. <dt>A-0</dt>
  2024. <dd>Fully preprocessor-ize the <tt class="docutils literal"><span class="pre">tiny</span></tt>
  2025. type sequence implemented in Chapter 5 so that all boilerplate code
  2026. is eliminated and the maximum size of a <tt class="docutils literal"><span
  2027. class="pre">tiny</span></tt> sequence can be adjusted by
  2028. changing <tt class="docutils literal"><span class="pre">TINY_MAX_SIZE</span></tt>.</dd>
  2029. </dl>
  2030. <!-- on hold:
  2031. It isn't uncommon to need token-wise arithmetic operations forpurposes other than invoking Preprocessor Library repetitionmacros. For example, let's write a metafunction to generate
  2032. function types from "signature" type sequences that specify thefunction's return and parameter types:: template <unsigned Size, class Signature>
  2033. struct to_function_impl; template <class Signature> struct to_function
  2034. : to_function_impl<mpl::size<Signature>::type, Signature> {};The challenge now is to implement ``to_function_impl``. For
  2035. ``Size == 3``, an appropriate specialization might look like this:: template <class Signature> struct to_function_impl<3,Signature>
  2036. { typedef mpl::begin<Signature>::type i0; typedef mpl::deref<i0>::type t0;
  2037. typedef mpl::next<i0>::type i1; typedef mpl::deref<i1>::type t1; typedef mpl::next<i1>::type i2;
  2038. typedef mpl::deref<i2>::type t2; typedef t0 type(t1,t2); };
  2039. A local macro to generate a single ``to_function_impl``specialization would look something like this:
  2040. .. parsed-literal:: #define to_function_impl_spec(size) \\ template <class Signature> \\
  2041. struct to_function_impl<3,Signature> \\ { \\ typedef mpl::begin<Signature>::type i0; \\ typedef mpl::deref<i0>::type t0; \\
  2042. \\ BOOST_PP_REPEAT_FROM_TO(1, size, to_function_t, ~) \\ \\ typedef t0 type(BOOST_PP_ENUM_SHIFTED_PARAMS(size,t)); \\
  2043. }; #define to_function_t(z, n, unused) \\ typedef mpl::next<BOOST_PP_CAT(i,\ **BOOST_PP_DEC(n)**)>::type \\
  2044. BOOST_PP_CAT(i,n); \\ \\ typedef mpl::deref<BOOST_PP_CAT(i,n)>::type BOOST_PP_CAT(t,n);
  2045. We've used some new library macros above; here is a brief rundown:* ``BOOST_PP_REPEAT_FROM_TO`` is just like ``BOOST_PP_REPEAT``, except that it accepts an initial repetition index. Since every
  2046. function has a return type, we don't need to worry about the case where ``Size == 0``.* ``BOOST_PP_ENUM_SHIFTED_PARAMS`` is just like
  2047. ``BOOST_PP_ENUM_PARAMS``, except that repetition indices start at ``1`` instead of ``0``.* ``BOOST_PP_CAT`` implements token pasting; its two arguments are
  2048. "glued" together into a single token. Since this is a general-purpose macro, it sits in ``cat.hpp`` at the top level of the library's directory tree. [#paste]_
  2049. .. [#paste] The preprocessor's built-in token-pasting operator, ``##``, often yields surprising results by taking effect before its arguments have been expanded. By contrast, ``BOOST_PP_CAT`` delays concatenation until after its arguments have been fully
  2050. evaluated.* Finally, though it only performs trivial arithmetic, ``BOOST_PP_DEC`` plays a crucial role in generating an
  2051. appropriate prior iterator identifier for our own code in ``to_function_t``.If we didn't have ``BOOST_PP_REPEAT_FROM_TO`` at our disposal in
  2052. the previous example, we might've had to use ``BOOST_PP_REPEAT``,which always starts iterating at ``0``. Consequently``to_function_t`` would've been responsible for producing thedeclarations of ``i0`` and ``t0`` as well as those of the other
  2053. nested types. To manage that, it would need a way to selectdifferent expansions depending on the value of ``n``.In its ``control/`` directory, the Preprocessor Library supplies a
  2054. macro ``BOOST_PP_IF(c,t,f)`` that fulfills a similar role to theone filled by ``mpl::if_``. Rewriting the example accordingly, weget:
  2055. .. parsed-literal:: #define to_function_impl_spec(size) \\ template <class Signature> \\
  2056. struct to_function_impl<3,Signature> \\ { \\ BOOST_PP_REPEAT_FROM_TO(1, size, to_function_t, ~) \\ \\
  2057. typedef t0 type(BOOST_PP_ENUM_SHIFTED_PARAMS(size,t)); \\ }; #define to_function_t(z, n, unused) \\
  2058. typedef BOOST_PP_IF( \\ n, \\ mpl::next<BOOST_PP_CAT(i,BOOST_PP_DEC(n))>::type, \\ typedef mpl::begin<Signature>::type i0; \\
  2059. ) \\ BOOST_PP_CAT(i,n); \\ \\ typedef mpl::deref<BOOST_PP_CAT(i,n)>::type BOOST_PP_CAT(t,n);
  2060. Although the formulation above will work, it does unnecessary workwhen ``n == 0``, evaluating the "true" branch of the conditionalonly to discard it. -->
  2061. </div>
  2062. </div>
  2063. <hr class="docutils footer" />
  2064. <div class="footer"> Generated on: 2005-10-17 19:34 UTC. Generated by <a class="reference"
  2065. href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference"
  2066. href="http://docutils.sourceforge.net/rst.html">reStructuredText</a>
  2067. source. </div>
  2068. </body>
  2069. </html>
  2070. <!--
  2071. FILE ARCHIVED ON 19:59:10 Mar 30, 2013 AND RETRIEVED FROM THE INTERNET ARCHIVE ON 21:06:19 May 19, 2015. JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.
  2072. ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C. SECTION 108(a)(3)).-->