index.html 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.12: http://docutils.sourceforge.net/" />
  7. <title>The Boost Parameter Library</title>
  8. <link rel="stylesheet" href="rst.css" type="text/css" />
  9. </head>
  10. <body>
  11. <div class="document" id="the-boost-parameter-library">
  12. <h1 class="title">The Boost Parameter Library</h1>
  13. <p><a class="reference external" href="../../../../index.htm"><img alt="Boost" src="../../../../boost.png" /></a></p>
  14. <hr class="docutils" />
  15. <table class="docutils field-list" frame="void" rules="none">
  16. <col class="field-name" />
  17. <col class="field-body" />
  18. <tbody valign="top">
  19. <tr class="field"><th class="field-name">Abstract:</th><td class="field-body">Use this library to write functions and class templates that can
  20. accept arguments by name:</td>
  21. </tr>
  22. </tbody>
  23. </table>
  24. <pre class="literal-block">
  25. new_window(
  26. &quot;alert&quot;
  27. , <strong>_width=10</strong>
  28. , <strong>_titlebar=false</strong>
  29. );
  30. smart_ptr&lt;
  31. Foo
  32. , <strong>deleter&lt;Deallocate&lt;Foo&gt; &gt;</strong>
  33. , <strong>copy_policy&lt;DeepCopy&gt;</strong>
  34. &gt; p(new Foo);
  35. </pre>
  36. <p>Since named arguments can be passed in any order, they are especially useful
  37. when a function or template has more than one parameter with a useful default
  38. value. The library also supports <em>deduced</em> parameters: that is to say,
  39. parameters whose identity can be deduced from their types.</p>
  40. <!-- @jam_prefix.append('''
  41. project test
  42. : requirements <include>. <implicit-dependency>/boost//headers ;
  43. ''') -->
  44. <!-- @example.prepend('''
  45. #include <boost/parameter.hpp>
  46. namespace test {
  47. BOOST_PARAMETER_NAME(title)
  48. BOOST_PARAMETER_NAME(width)
  49. BOOST_PARAMETER_NAME(titlebar)
  50. BOOST_PARAMETER_FUNCTION(
  51. (int), new_window, tag, (required (title,*)(width,*)(titlebar,*))
  52. )
  53. {
  54. return 0;
  55. }
  56. BOOST_PARAMETER_TEMPLATE_KEYWORD(deleter)
  57. BOOST_PARAMETER_TEMPLATE_KEYWORD(copy_policy)
  58. template <typename T>
  59. struct Deallocate
  60. {
  61. };
  62. struct DeepCopy
  63. {
  64. };
  65. namespace parameter = boost::parameter;
  66. struct Foo
  67. {
  68. };
  69. template <typename T, typename A0, typename A1>
  70. struct smart_ptr
  71. {
  72. smart_ptr(Foo*);
  73. };
  74. }
  75. using namespace test;
  76. int x =
  77. '''); -->
  78. <!-- @test('compile') -->
  79. <hr class="docutils" />
  80. <table class="docutils field-list" frame="void" rules="none">
  81. <col class="field-name" />
  82. <col class="field-body" />
  83. <tbody valign="top">
  84. <tr class="field"><th class="field-name">Authors:</th><td class="field-body">David Abrahams, Daniel Wallin</td>
  85. </tr>
  86. <tr class="field"><th class="field-name">Contact:</th><td class="field-body"><a class="reference external" href="mailto:dave&#64;boost-consulting.com">dave&#64;boost-consulting.com</a>, <a class="reference external" href="mailto:daniel&#64;boostpro.com">daniel&#64;boostpro.com</a></td>
  87. </tr>
  88. <tr class="field"><th class="field-name">organization:</th><td class="field-body"><a class="reference external" href="http://www.boostpro.com">BoostPro Computing</a></td>
  89. </tr>
  90. <tr class="field"><th class="field-name">date:</th><td class="field-body">$Date: 2005/07/17 19:53:01 $</td>
  91. </tr>
  92. <tr class="field"><th class="field-name">copyright:</th><td class="field-body">Copyright David Abrahams, Daniel Wallin
  93. 2005-2009. Distributed under the Boost Software License,
  94. Version 1.0. (See accompanying file LICENSE_1_0.txt
  95. or copy at <a class="reference external" href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</td>
  96. </tr>
  97. </tbody>
  98. </table>
  99. <hr class="docutils" />
  100. <p>[Note: this tutorial does not cover all details of the library. Please see
  101. also the <a class="reference external" href="reference.html">reference documentation</a>]</p>
  102. <div class="contents topic" id="table-of-contents">
  103. <p class="topic-title first"><strong>Table of Contents</strong></p>
  104. <ul class="auto-toc simple">
  105. <li><a class="reference internal" href="#motivation" id="id22">1&nbsp;&nbsp;&nbsp;Motivation</a><ul class="auto-toc">
  106. <li><a class="reference internal" href="#named-function-parameters" id="id23">1.1&nbsp;&nbsp;&nbsp;Named Function Parameters</a></li>
  107. <li><a class="reference internal" href="#deduced-function-parameters" id="id24">1.2&nbsp;&nbsp;&nbsp;Deduced Function Parameters</a></li>
  108. <li><a class="reference internal" href="#class-template-parameter-support" id="id25">1.3&nbsp;&nbsp;&nbsp;Class Template Parameter Support</a></li>
  109. </ul>
  110. </li>
  111. <li><a class="reference internal" href="#tutorial" id="id26">2&nbsp;&nbsp;&nbsp;Tutorial</a><ul class="auto-toc">
  112. <li><a class="reference internal" href="#parameter-enabled-functions" id="id27">2.1&nbsp;&nbsp;&nbsp;Parameter-Enabled Functions</a></li>
  113. <li><a class="reference internal" href="#parameter-enabled-member-functions" id="id28">2.2&nbsp;&nbsp;&nbsp;Parameter-Enabled Member Functions</a></li>
  114. <li><a class="reference internal" href="#parameter-enabled-function-call-operators" id="id29">2.3&nbsp;&nbsp;&nbsp;Parameter-Enabled Function Call Operators</a></li>
  115. <li><a class="reference internal" href="#parameter-enabled-constructors" id="id30">2.4&nbsp;&nbsp;&nbsp;Parameter-Enabled Constructors</a></li>
  116. <li><a class="reference internal" href="#parameter-enabled-class-templates" id="id31">2.5&nbsp;&nbsp;&nbsp;Parameter-Enabled Class Templates</a></li>
  117. </ul>
  118. </li>
  119. <li><a class="reference internal" href="#advanced-topics" id="id32">3&nbsp;&nbsp;&nbsp;Advanced Topics</a><ul class="auto-toc">
  120. <li><a class="reference internal" href="#fine-grained-name-control" id="id33">3.1&nbsp;&nbsp;&nbsp;Fine-Grained Name Control</a></li>
  121. <li><a class="reference internal" href="#more-argumentpacks" id="id34">3.2&nbsp;&nbsp;&nbsp;More <span class="concept">ArgumentPack</span>s</a></li>
  122. </ul>
  123. </li>
  124. <li><a class="reference internal" href="#best-practices" id="id35">4&nbsp;&nbsp;&nbsp;Best Practices</a><ul class="auto-toc">
  125. <li><a class="reference internal" href="#keyword-naming" id="id36">4.1&nbsp;&nbsp;&nbsp;Keyword Naming</a></li>
  126. <li><a class="reference internal" href="#namespaces" id="id37">4.2&nbsp;&nbsp;&nbsp;Namespaces</a></li>
  127. <li><a class="reference internal" href="#documentation" id="id38">4.3&nbsp;&nbsp;&nbsp;Documentation</a></li>
  128. </ul>
  129. </li>
  130. <li><a class="reference internal" href="#portability-considerations" id="id39">5&nbsp;&nbsp;&nbsp;Portability Considerations</a><ul class="auto-toc">
  131. <li><a class="reference internal" href="#perfect-forwarding-support" id="id40">5.1&nbsp;&nbsp;&nbsp;Perfect Forwarding Support</a></li>
  132. <li><a class="reference internal" href="#boost-mp11-support" id="id41">5.2&nbsp;&nbsp;&nbsp;Boost.MP11 Support</a></li>
  133. <li><a class="reference internal" href="#no-sfinae-support" id="id42">5.3&nbsp;&nbsp;&nbsp;No SFINAE Support</a></li>
  134. <li><a class="reference internal" href="#no-support-for-result-of" id="id43">5.4&nbsp;&nbsp;&nbsp;No Support for <tt class="docutils literal">result_of</tt></a></li>
  135. <li><a class="reference internal" href="#compiler-can-t-see-references-in-unnamed-namespace" id="id44">5.5&nbsp;&nbsp;&nbsp;Compiler Can't See References In Unnamed Namespace</a></li>
  136. </ul>
  137. </li>
  138. <li><a class="reference internal" href="#python-binding" id="id45">6&nbsp;&nbsp;&nbsp;Python Binding</a></li>
  139. <li><a class="reference internal" href="#reference" id="id46">7&nbsp;&nbsp;&nbsp;Reference</a></li>
  140. <li><a class="reference internal" href="#glossary" id="id47">8&nbsp;&nbsp;&nbsp;Glossary</a><ul class="auto-toc">
  141. <li><a class="reference internal" href="#argument-or-actual-argument" id="id48">8.1&nbsp;&nbsp;&nbsp;Argument (or “actual argument”)</a></li>
  142. <li><a class="reference internal" href="#parameter-or-formal-parameter" id="id49">8.2&nbsp;&nbsp;&nbsp;Parameter (or “formal parameter”)</a></li>
  143. </ul>
  144. </li>
  145. <li><a class="reference internal" href="#acknowledgements" id="id50">9&nbsp;&nbsp;&nbsp;Acknowledgements</a></li>
  146. </ul>
  147. </div>
  148. <hr class="docutils" />
  149. <div class="section" id="motivation">
  150. <h1><a class="toc-backref" href="#id22">1&nbsp;&nbsp;&nbsp;Motivation</a></h1>
  151. <p>In C++, <a class="reference internal" href="#arguments">arguments</a> are normally given meaning by their positions with respect
  152. to a <a class="reference internal" href="#parameter">parameter</a> list: the first argument passed maps onto the first parameter
  153. in a function's definition, and so on. That protocol is fine when there is at
  154. most one parameter with a default value, but when there are even a few useful
  155. defaults, the positional interface becomes burdensome:</p>
  156. <ul>
  157. <li><div class="first compound">
  158. <p class="compound-first">Since an argument's meaning is given by its position, we have to choose an
  159. (often arbitrary) order for parameters with default values, making some
  160. combinations of defaults unusable:</p>
  161. <pre class="compound-middle literal-block">
  162. window* new_window(
  163. char const* name
  164. , <strong>int border_width = default_border_width</strong>
  165. , bool movable = true
  166. , bool initially_visible = true
  167. );
  168. bool const movability = false;
  169. window* w = new_window(&quot;alert box&quot;, movability);
  170. </pre>
  171. <p class="compound-middle">In the example above we wanted to make an unmoveable window with a default
  172. <tt class="docutils literal">border_width</tt>, but instead we got a moveable window with a
  173. <tt class="docutils literal">border_width</tt> of zero. To get the desired effect, we'd need to write:</p>
  174. <pre class="compound-last literal-block">
  175. window* w = new_window(
  176. &quot;alert box&quot;, <strong>default_border_width</strong>, movability
  177. );
  178. </pre>
  179. </div>
  180. </li>
  181. <li><div class="first compound">
  182. <p class="compound-first">It can become difficult for readers to understand the meaning of arguments
  183. at the call site:</p>
  184. <pre class="compound-middle literal-block">
  185. window* w = new_window(&quot;alert&quot;, 1, true, false);
  186. </pre>
  187. <p class="compound-last">Is this window moveable and initially invisible, or unmoveable and
  188. initially visible? The reader needs to remember the order of arguments to
  189. be sure.</p>
  190. </div>
  191. </li>
  192. <li><p class="first">The author of the call may not remember the order of the arguments either,
  193. leading to hard-to-find bugs.</p>
  194. </li>
  195. </ul>
  196. <!-- @ignore(3) -->
  197. <div class="section" id="named-function-parameters">
  198. <h2><a class="toc-backref" href="#id23">1.1&nbsp;&nbsp;&nbsp;Named Function Parameters</a></h2>
  199. <div class="compound">
  200. <p class="compound-first">This library addresses the problems outlined above by associating each
  201. parameter name with a keyword object. Now users can identify arguments by
  202. name, rather than by position:</p>
  203. <pre class="compound-last literal-block">
  204. window* w = new_window(
  205. &quot;alert box&quot;
  206. , <strong>movable_=</strong>false
  207. ); // OK!
  208. </pre>
  209. </div>
  210. <!-- @ignore() -->
  211. </div>
  212. <div class="section" id="deduced-function-parameters">
  213. <h2><a class="toc-backref" href="#id24">1.2&nbsp;&nbsp;&nbsp;Deduced Function Parameters</a></h2>
  214. <div class="compound">
  215. <p class="compound-first">A <strong>deduced parameter</strong> can be passed in any position <em>without</em> supplying
  216. an explicit parameter name. It's not uncommon for a function to have
  217. parameters that can be uniquely identified based on the types of arguments
  218. passed. The <tt class="docutils literal">name</tt> parameter to <tt class="docutils literal">new_window</tt> is one such
  219. example. None of the other arguments, if valid, can reasonably be
  220. converted to a <tt class="docutils literal">char const*</tt>. With a deduced parameter interface, we
  221. could pass the window name in <em>any</em> argument position without causing
  222. ambiguity:</p>
  223. <pre class="compound-middle literal-block">
  224. window* w = new_window(
  225. movable_=false
  226. , <strong>&quot;alert box&quot;</strong>
  227. ); // OK!
  228. window* w = new_window(
  229. <strong>&quot;alert box&quot;</strong>
  230. , movable_=false
  231. ); // OK!
  232. </pre>
  233. <p class="compound-last">Appropriately used, a deduced parameter interface can free the user of the
  234. burden of even remembering the formal parameter names.</p>
  235. </div>
  236. <!-- @ignore() -->
  237. </div>
  238. <div class="section" id="class-template-parameter-support">
  239. <h2><a class="toc-backref" href="#id25">1.3&nbsp;&nbsp;&nbsp;Class Template Parameter Support</a></h2>
  240. <div class="compound">
  241. <p class="compound-first">The reasoning we've given for named and deduced parameter interfaces
  242. applies equally well to class templates as it does to functions. Using
  243. the Parameter library, we can create interfaces that allow template
  244. arguments (in this case <tt class="docutils literal">shared</tt> and <tt class="docutils literal">Client</tt>) to be explicitly named,
  245. like this:</p>
  246. <pre class="compound-middle literal-block">
  247. smart_ptr&lt;
  248. <strong>ownership&lt;shared&gt;</strong>
  249. , <strong>value_type&lt;Client&gt;</strong>
  250. &gt; p;
  251. </pre>
  252. <p class="compound-middle">The syntax for passing named template arguments is not quite as natural as
  253. it is for function arguments (ideally, we'd be able to write
  254. <tt class="docutils literal">smart_ptr&lt;ownership = shared, …&gt;</tt>). This small syntactic deficiency
  255. makes deduced parameters an especially big win when used with class
  256. templates:</p>
  257. <pre class="compound-last literal-block">
  258. // <em>p and q could be equivalent, given a deduced</em>
  259. // <em>parameter interface.</em>
  260. smart_ptr&lt;<strong>shared</strong>, <strong>Client</strong>&gt; p;
  261. smart_ptr&lt;<strong>Client</strong>, <strong>shared</strong>&gt; q;
  262. </pre>
  263. </div>
  264. <!-- @ignore(2) -->
  265. </div>
  266. </div>
  267. <div class="section" id="tutorial">
  268. <h1><a class="toc-backref" href="#id26">2&nbsp;&nbsp;&nbsp;Tutorial</a></h1>
  269. <p>This tutorial shows all the basics—how to build both named- and
  270. deduced-parameter interfaces to function templates and class
  271. templates—and several more advanced idioms as well.</p>
  272. <div class="section" id="parameter-enabled-functions">
  273. <h2><a class="toc-backref" href="#id27">2.1&nbsp;&nbsp;&nbsp;Parameter-Enabled Functions</a></h2>
  274. <p>In this section we'll show how the Parameter library can be used to
  275. build an expressive interface to the <a class="reference external" href="../../../graph/doc/index.html">Boost Graph library</a>'s
  276. <a class="reference external" href="../../../graph/doc/depth_first_search.html"><tt class="docutils literal">depth_first_search</tt></a> algorithm.<a class="footnote-reference" href="#old-interface" id="id3"><sup>1</sup></a></p>
  277. <!-- Revisit this
  278. After laying some groundwork and describing the algorithm's abstract
  279. interface, we'll show you how to build a basic implementation with keyword
  280. support. Then we'll add support for default arguments and we'll gradually
  281. refine the implementation with syntax improvements. Finally we'll show
  282. how to streamline the implementation of named parameter interfaces,
  283. improve their participation in overload resolution, and optimize their
  284. runtime efficiency. -->
  285. <div class="section" id="headers-and-namespaces">
  286. <h3>2.1.1&nbsp;&nbsp;&nbsp;Headers And Namespaces</h3>
  287. <p>Most components of the Parameter library are declared in a header named for
  288. the component. For example,</p>
  289. <pre class="literal-block">
  290. #include &lt;boost/parameter/keyword.hpp&gt;
  291. </pre>
  292. <p>will ensure <tt class="docutils literal"><span class="pre">boost::parameter::keyword</span></tt> is known to the compiler. There
  293. is also a combined header, <tt class="docutils literal">boost/parameter.hpp</tt>, that includes most of
  294. the library's components. For the the rest of this tutorial, unless we
  295. say otherwise, you can use the rule above to figure out which header to
  296. <tt class="docutils literal">#include</tt> to access any given component of the library.</p>
  297. <!-- @example.append('''
  298. using boost::parameter::keyword;
  299. ''') -->
  300. <!-- @test('compile') -->
  301. <p>Also, the examples below will also be written as if the namespace alias</p>
  302. <pre class="literal-block">
  303. namespace parameter = boost::parameter;
  304. </pre>
  305. <!-- @ignore() -->
  306. <p>has been declared: we'll write <tt class="docutils literal"><span class="pre">parameter::xxx</span></tt> instead of
  307. <tt class="docutils literal"><span class="pre">boost::parameter::xxx</span></tt>.</p>
  308. </div>
  309. <div class="section" id="the-abstract-interface-to-dfs">
  310. <h3>2.1.2&nbsp;&nbsp;&nbsp;The Abstract Interface to <tt class="docutils literal">depth_first_search</tt></h3>
  311. <p>The Graph library's <tt class="docutils literal">depth_first_search</tt> algorithm is a generic function accepting
  312. from one to four arguments by reference. If all arguments were
  313. required, its signature might be as follows:</p>
  314. <pre class="literal-block">
  315. template &lt;
  316. typename Graph
  317. , typename DFSVisitor
  318. , typename Index
  319. , typename ColorMap
  320. &gt;
  321. void
  322. depth_first_search(
  323. Graph const&amp; graph
  324. , DFSVisitor visitor
  325. , typename graph_traits&lt;g&gt;::vertex_descriptor root_vertex
  326. , IndexMap index_map
  327. , ColorMap&amp; color
  328. );
  329. </pre>
  330. <!-- @ignore() -->
  331. <p>However, most of the parameters have a useful default value,
  332. as shown in the table below.</p>
  333. <table border="1" class="docutils" id="default-expressions">
  334. <span id="parameter-table"></span><caption><tt class="docutils literal">depth_first_search</tt> Parameters</caption>
  335. <colgroup>
  336. <col width="20%" />
  337. <col width="7%" />
  338. <col width="30%" />
  339. <col width="43%" />
  340. </colgroup>
  341. <thead valign="bottom">
  342. <tr><th class="head">Parameter
  343. Name</th>
  344. <th class="head">Data
  345. Flow</th>
  346. <th class="head">Type</th>
  347. <th class="head">Default Value
  348. (if any)</th>
  349. </tr>
  350. </thead>
  351. <tbody valign="top">
  352. <tr><td><tt class="docutils literal">graph</tt></td>
  353. <td>in</td>
  354. <td>Model of
  355. <a class="reference external" href="../../../graph/doc/IncidenceGraph.html"><span class="concept">Incidence Graph</span></a> and
  356. <a class="reference external" href="../../../graph/doc/VertexListGraph.html"><span class="concept">Vertex List Graph</span></a></td>
  357. <td>none - this argument is required.</td>
  358. </tr>
  359. <tr><td><tt class="docutils literal">visitor</tt></td>
  360. <td>in</td>
  361. <td>Model of <a class="reference external" href="../../../graph/doc/DFSVisitor.html"><span class="concept">DFS Visitor</span></a></td>
  362. <td><tt class="docutils literal"><span class="pre">boost::dfs_visitor&lt;&gt;()</span></tt></td>
  363. </tr>
  364. <tr><td><tt class="docutils literal">root_vertex</tt></td>
  365. <td>in</td>
  366. <td><tt class="docutils literal">graph</tt>'s vertex
  367. descriptor type.</td>
  368. <td><tt class="docutils literal"><span class="pre">*vertices(graph).first</span></tt></td>
  369. </tr>
  370. <tr><td><tt class="docutils literal">index_map</tt></td>
  371. <td>in</td>
  372. <td>Model of
  373. <a class="reference external" href="../../../property_map/doc/ReadablePropertyMap.html"><span class="concept">Readable Property Map</span></a>
  374. with key type :=
  375. <tt class="docutils literal">graph</tt>'s vertex
  376. descriptor and value
  377. type an integer type.</td>
  378. <td><tt class="docutils literal"><span class="pre">get(boost::vertex_index,graph)</span></tt></td>
  379. </tr>
  380. <tr><td><tt class="docutils literal">color_map</tt></td>
  381. <td>in /
  382. out</td>
  383. <td>Model of
  384. <a class="reference external" href="../../../property_map/doc/ReadWritePropertyMap.html"><span class="concept">Read/Write Property Map</span></a>
  385. with key type :=
  386. <tt class="docutils literal">graph</tt>'s vertex
  387. descriptor type.</td>
  388. <td>a <tt class="docutils literal"><span class="pre">boost::iterator_property_map</span></tt>
  389. created from a <tt class="docutils literal"><span class="pre">std::vector</span></tt> of
  390. <tt class="docutils literal">default_color_type</tt> of size
  391. <tt class="docutils literal">num_vertices(graph)</tt> and using
  392. <tt class="docutils literal">index_map</tt> for the index map.</td>
  393. </tr>
  394. </tbody>
  395. </table>
  396. <p>Don't be intimidated by the information in the second and third columns
  397. above. For the purposes of this exercise, you don't need to understand
  398. them in detail.</p>
  399. </div>
  400. <div class="section" id="defining-the-keywords">
  401. <h3>2.1.3&nbsp;&nbsp;&nbsp;Defining the Keywords</h3>
  402. <p>The point of this exercise is to make it possible to call
  403. <tt class="docutils literal">depth_first_search</tt> with named arguments, leaving out any
  404. arguments for which the default is appropriate:</p>
  405. <pre class="literal-block">
  406. graphs::depth_first_search(g, <strong>color_map_=my_color_map</strong>);
  407. </pre>
  408. <!-- @ignore() -->
  409. <p>To make that syntax legal, there needs to be an object called
  410. “<tt class="docutils literal">color_map_</tt>” whose assignment operator can accept a
  411. <tt class="docutils literal">my_color_map</tt> argument. In this step we'll create one such
  412. <strong>keyword object</strong> for each parameter. Each keyword object will be
  413. identified by a unique <strong>keyword tag type</strong>.</p>
  414. <!-- Revisit this
  415. We're going to define our interface in namespace ``graphs``. Since users
  416. need access to the keyword objects, but not the tag types, we'll define
  417. the keyword objects so they're accessible through ``graphs``, and we'll
  418. hide the tag types away in a nested namespace, ``graphs::tag``. The
  419. library provides a convenient macro for that purpose. -->
  420. <p>We're going to define our interface in namespace <tt class="docutils literal">graphs</tt>. The
  421. library provides a convenient macro for defining keyword objects:</p>
  422. <pre class="literal-block">
  423. #include &lt;boost/parameter/name.hpp&gt;
  424. namespace graphs {
  425. BOOST_PARAMETER_NAME(graph) // Note: no semicolon
  426. BOOST_PARAMETER_NAME(visitor)
  427. BOOST_PARAMETER_NAME(root_vertex)
  428. BOOST_PARAMETER_NAME(index_map)
  429. BOOST_PARAMETER_NAME(color_map)
  430. }
  431. </pre>
  432. <!-- @test('compile') -->
  433. <p>The declaration of the <tt class="docutils literal">graph</tt> keyword you see here is equivalent to:</p>
  434. <pre class="literal-block">
  435. namespace graphs {
  436. namespace tag {
  437. // keyword tag type
  438. struct graph
  439. {
  440. typedef boost::parameter::forward_reference qualifier;
  441. };
  442. }
  443. namespace // unnamed
  444. {
  445. // A reference to the keyword object
  446. boost::parameter::keyword&lt;tag::graph&gt; const&amp; _graph
  447. = boost::parameter::keyword&lt;tag::graph&gt;::instance;
  448. }
  449. }
  450. </pre>
  451. <!-- @example.prepend('#include <boost/parameter/keyword.hpp>') -->
  452. <!-- @test('compile') -->
  453. <p>It defines a <em>keyword tag type</em> named <tt class="docutils literal"><span class="pre">tag::graph</span></tt> and a <em>keyword object</em>
  454. reference named <tt class="docutils literal">_graph</tt>.</p>
  455. <p>This “fancy dance” involving an unnamed namespace and references is all done
  456. to avoid violating the One Definition Rule (ODR)<a class="footnote-reference" href="#odr" id="id5"><sup>2</sup></a> when the named
  457. parameter interface is used by function templates that are instantiated in
  458. multiple translation units (MSVC6.x users see <a class="reference internal" href="#compiler-can-t-see-references-in-unnamed-namespace">this note</a>).</p>
  459. </div>
  460. <div class="section" id="writing-the-function">
  461. <h3>2.1.4&nbsp;&nbsp;&nbsp;Writing the Function</h3>
  462. <p>Now that we have our keywords defined, the function template definition
  463. follows a simple pattern using the <tt class="docutils literal">BOOST_PARAMETER_FUNCTION</tt> macro:</p>
  464. <pre class="literal-block">
  465. #include &lt;boost/parameter/preprocessor.hpp&gt;
  466. namespace graphs {
  467. BOOST_PARAMETER_FUNCTION(
  468. (void), // 1. parenthesized return type
  469. depth_first_search, // 2. name of the function template
  470. tag, // 3. namespace of tag types
  471. (required (graph, *) ) // 4. one required parameter, and
  472. (optional // four optional parameters,
  473. // with defaults
  474. (visitor, *, boost::dfs_visitor&lt;&gt;())
  475. (root_vertex, *, *vertices(graph).first)
  476. (index_map, *, get(boost::vertex_index,graph))
  477. (color_map, *,
  478. default_color_map(num_vertices(graph), index_map)
  479. )
  480. )
  481. )
  482. {
  483. // ... body of function goes here...
  484. // use graph, visitor, index_map, and color_map
  485. }
  486. }
  487. </pre>
  488. <!-- @example.prepend('''
  489. #include <boost/parameter/name.hpp>
  490. BOOST_PARAMETER_NAME(graph)
  491. BOOST_PARAMETER_NAME(visitor)
  492. BOOST_PARAMETER_NAME(in(root_vertex))
  493. BOOST_PARAMETER_NAME(in(index_map))
  494. BOOST_PARAMETER_NAME(in_out(color_map))
  495. namespace boost {
  496. template <typename T = int>
  497. struct dfs_visitor
  498. {
  499. };
  500. int vertex_index = 0;
  501. }
  502. ''') -->
  503. <!-- @test('compile') -->
  504. <p>The arguments to <tt class="docutils literal">BOOST_PARAMETER_FUNCTION</tt> are:</p>
  505. <ol class="arabic simple">
  506. <li>The return type of the resulting function template. Parentheses around
  507. the return type prevent any commas it might contain from confusing the
  508. preprocessor, and are always required.</li>
  509. <li>The name of the resulting function template.</li>
  510. <li>The name of a namespace where we can find tag types whose names match the
  511. function's parameter names.</li>
  512. <li>The function signature.</li>
  513. </ol>
  514. </div>
  515. <div class="section" id="function-signatures">
  516. <h3>2.1.5&nbsp;&nbsp;&nbsp;Function Signatures</h3>
  517. <p>Function signatures are described as one or two adjacent parenthesized terms
  518. (a <a class="reference external" href="../../../preprocessor/doc/index.html">Boost.Preprocessor</a> <a class="reference external" href="http://boost-consulting.com/mplbook/preprocessor.html#sequences">sequence</a>) describing the function's parameters in the
  519. order in which they'd be expected if passed positionally. Any required
  520. parameters must come first, but the <tt class="docutils literal">(required … )</tt> clause can be omitted
  521. when all the parameters are optional.</p>
  522. <div class="section" id="required-parameters">
  523. <h4>2.1.5.1&nbsp;&nbsp;&nbsp;Required Parameters</h4>
  524. <div class="compound">
  525. <p class="compound-first">Required parameters are given first—nested in a <tt class="docutils literal">(required … )</tt>
  526. clause—as a series of two-element tuples describing each parameter name
  527. and any requirements on the argument type. In this case there is only a
  528. single required parameter, so there's just a single tuple:</p>
  529. <pre class="compound-middle literal-block">
  530. (required <strong>(graph, *)</strong> )
  531. </pre>
  532. <p class="compound-last">Since <tt class="docutils literal">depth_first_search</tt> doesn't require any particular type for its
  533. <tt class="docutils literal">graph</tt> parameter, we use an asterix to indicate that any type is
  534. allowed. Required parameters must always precede any optional parameters
  535. in a signature, but if there are <em>no</em> required parameters, the
  536. <tt class="docutils literal">(required … )</tt> clause can be omitted entirely.</p>
  537. </div>
  538. <!-- @example.prepend('''
  539. #include <boost/parameter.hpp>
  540. BOOST_PARAMETER_NAME(graph)
  541. BOOST_PARAMETER_FUNCTION((void), f, tag,
  542. ''') -->
  543. <!-- @example.append(') {}') -->
  544. <!-- @test('compile') -->
  545. </div>
  546. <div class="section" id="optional-parameters">
  547. <h4>2.1.5.2&nbsp;&nbsp;&nbsp;Optional Parameters</h4>
  548. <div class="compound">
  549. <p class="compound-first">Optional parameters—nested in an <tt class="docutils literal">(optional … )</tt> clause—are given as a
  550. series of adjacent <em>three</em>-element tuples describing the parameter name,
  551. any requirements on the argument type, <em>and</em> and an expression
  552. representing the parameter's default value:</p>
  553. <pre class="compound-last literal-block">
  554. (optional
  555. <strong>(visitor, *, boost::dfs_visitor&lt;&gt;())
  556. (root_vertex, *, *vertices(graph).first)
  557. (index_map, *, get(boost::vertex_index,graph))
  558. (color_map, *,
  559. default_color_map(num_vertices(graph), index_map)
  560. )</strong>
  561. )
  562. </pre>
  563. </div>
  564. <!-- @example.prepend('''
  565. #include <boost/parameter.hpp>
  566. namespace boost {
  567. int vertex_index = 0;
  568. template <typename T = int>
  569. struct dfs_visitor
  570. {
  571. };
  572. }
  573. BOOST_PARAMETER_NAME(graph)
  574. BOOST_PARAMETER_NAME(visitor)
  575. BOOST_PARAMETER_NAME(in(root_vertex))
  576. BOOST_PARAMETER_NAME(in(index_map))
  577. BOOST_PARAMETER_NAME(in_out(color_map))
  578. BOOST_PARAMETER_FUNCTION((void), f, tag,
  579. (required (graph, \*))
  580. ''') -->
  581. <!-- @example.append(') {}') -->
  582. <!-- @test('compile') -->
  583. </div>
  584. <div class="section" id="handling-in-out-consume-move-from-and-forward-parameters">
  585. <h4>2.1.5.3&nbsp;&nbsp;&nbsp;Handling “In”, “Out”, “Consume / Move-From”, and “Forward” Parameters</h4>
  586. <div class="compound">
  587. <p class="compound-first">By default, Boost.Parameter treats all parameters as if they were
  588. <em>forward</em> <a class="reference external" href="http://www.modernescpp.com/index.php/c-core-guidelines-how-to-pass-function-parameters">parameters</a>, which functions would take in by rvalue reference
  589. and only <tt class="docutils literal"><span class="pre">std::forward</span></tt> or <tt class="docutils literal"><span class="pre">boost::forward</span></tt> to other functions. Such
  590. parameters can be <tt class="docutils literal">const</tt> lvalues, mutable lvalues, <tt class="docutils literal">const</tt> rvalues,
  591. or mutable rvalues. Therefore, the default configuration grants the most
  592. flexibility to user code. However:</p>
  593. <ul class="compound-middle simple">
  594. <li>Users can configure one or more parameters to be <em>in</em> <a class="reference external" href="http://www.modernescpp.com/index.php/c-core-guidelines-how-to-pass-function-parameters">parameters</a>,
  595. which can fall into the same categories as <em>forward</em> <a class="reference external" href="http://www.modernescpp.com/index.php/c-core-guidelines-how-to-pass-function-parameters">parameters</a> but
  596. are now passed by <tt class="docutils literal">const</tt> lvalue reference and so must only be read
  597. from. Continuing from the previous example, to indicate that
  598. <tt class="docutils literal">root_vertex</tt> and <tt class="docutils literal">index_map</tt> are read-only, we wrap their names
  599. in <tt class="docutils literal"><span class="pre">in(…)</span></tt>.</li>
  600. <li>Users can configure one or more parameters to be either <em>out</em>
  601. <a class="reference external" href="http://www.modernescpp.com/index.php/c-core-guidelines-how-to-pass-function-parameters">parameters</a>, which functions would strictly write to, or <em>in-out</em>
  602. <a class="reference external" href="http://www.modernescpp.com/index.php/c-core-guidelines-how-to-pass-function-parameters">parameters</a>, which functions would both read from and write
  603. to. Such parameters can only be mutable lvalues. In the example, to
  604. indicate that <tt class="docutils literal">color_map</tt> is read-write, we wrap its name in
  605. <tt class="docutils literal"><span class="pre">in_out(…)</span></tt>. Note that Boost.Parameter sees no functional
  606. difference between <tt class="docutils literal"><span class="pre">out(…)</span></tt> and <tt class="docutils literal"><span class="pre">in_out(…)</span></tt>, so you may choose
  607. whichever makes your interfaces more self-documenting.</li>
  608. <li>Users can configure one or more parameters to be <em>consume</em> or
  609. <em>move-from</em> <a class="reference external" href="http://www.modernescpp.com/index.php/c-core-guidelines-how-to-pass-function-parameters">parameters</a>, which functions would take in by mutable
  610. rvalue reference and <tt class="docutils literal"><span class="pre">std::move</span></tt> or <tt class="docutils literal"><span class="pre">boost::move</span></tt> as the last
  611. access step. Such parameters can only be mutable
  612. rvalues. Boost.Parameter supports wrapping the corresponding names in
  613. <tt class="docutils literal"><span class="pre">consume(…)</span></tt> or <tt class="docutils literal"><span class="pre">move_from(…)</span></tt>.</li>
  614. </ul>
  615. <pre class="compound-middle literal-block">
  616. BOOST_PARAMETER_NAME(graph)
  617. BOOST_PARAMETER_NAME(visitor)
  618. BOOST_PARAMETER_NAME(<strong>in(root_vertex)</strong>)
  619. BOOST_PARAMETER_NAME(<strong>in(index_map)</strong>)
  620. BOOST_PARAMETER_NAME(<strong>in_out(color_map)</strong>)
  621. </pre>
  622. <p class="compound-last">In order to see what happens when parameters are bound to arguments that
  623. violate their category constraints, attempt to compile the <a class="reference external" href="../../test/compose.cpp">compose.cpp</a>
  624. test program with either the <tt class="docutils literal">LIBS_PARAMETER_TEST_COMPILE_FAILURE_0</tt>
  625. macro or the <tt class="docutils literal">LIBS_PARAMETER_TEST_COMPILE_FAILURE_1</tt> macro
  626. <tt class="docutils literal">#defined</tt>. You should encounter a compiler error caused by a specific
  627. constraint violation.</p>
  628. </div>
  629. <!-- @example.prepend('''
  630. #include <boost/parameter.hpp>
  631. namespace boost {
  632. int vertex_index = 0;
  633. template <typename T = int>
  634. struct dfs_visitor
  635. {
  636. };
  637. }
  638. ''') -->
  639. <!-- @example.append('''
  640. BOOST_PARAMETER_FUNCTION((void), f, tag,
  641. (required (graph, \*))
  642. (optional
  643. (visitor, \*, boost::dfs_visitor<>())
  644. (root_vertex, \*, \*vertices(graph).first)
  645. (index_map, \*, get(boost::vertex_index, graph))
  646. (color_map, \*,
  647. default_color_map(num_vertices(graph), index_map)
  648. )
  649. )
  650. )
  651. {
  652. }
  653. ''') -->
  654. <!-- @test('compile') -->
  655. </div>
  656. <div class="section" id="positional-arguments">
  657. <h4>2.1.5.4&nbsp;&nbsp;&nbsp;Positional Arguments</h4>
  658. <p>When arguments are passed positionally (without the use of keywords), they
  659. will be mapped onto parameters in the order the parameters are given in the
  660. signature, so for example in this call</p>
  661. <pre class="literal-block">
  662. graphs::depth_first_search(x, y);
  663. </pre>
  664. <!-- @ignore() -->
  665. <p><tt class="docutils literal">x</tt> will always be interpreted as a graph and <tt class="docutils literal">y</tt> will always be
  666. interpreted as a visitor.</p>
  667. </div>
  668. <div class="section" id="default-expression-evaluation">
  669. <h4>2.1.5.5&nbsp;&nbsp;&nbsp;Default Expression Evaluation</h4>
  670. <div class="compound">
  671. <p class="compound-first">Note that in our example, the value of the graph parameter is used in the
  672. default expressions for <tt class="docutils literal">root_vertex</tt>, <tt class="docutils literal">index_map</tt>, and <tt class="docutils literal">color_map</tt>.</p>
  673. <pre class="compound-last literal-block">
  674. (required (<strong>graph</strong>, *) )
  675. (optional
  676. (visitor, *, boost::dfs_visitor&lt;&gt;())
  677. (root_vertex, *, *vertices(<strong>graph</strong>).first)
  678. (index_map, *, get(boost::vertex_index, <strong>graph</strong>))
  679. (color_map, *,
  680. default_color_map(num_vertices(<strong>graph</strong>), index_map)
  681. )
  682. )
  683. </pre>
  684. </div>
  685. <!-- @ignore()
  686. A default expression is evaluated in the context of all preceding
  687. parameters, so you can use any of their values by name. -->
  688. <div class="compound">
  689. <p class="compound-first">A default expression is never evaluated—or even instantiated—if an actual
  690. argument is passed for that parameter. We can actually demonstrate that
  691. with our code so far by replacing the body of <tt class="docutils literal">depth_first_search</tt> with
  692. something that prints the arguments:</p>
  693. <pre class="compound-middle literal-block">
  694. #include &lt;boost/graph/depth_first_search.hpp&gt; // for dfs_visitor
  695. BOOST_PARAMETER_FUNCTION(
  696. (bool), depth_first_search, tag
  697. <em>…signature goes here…</em>
  698. )
  699. {
  700. std::cout &lt;&lt; &quot;graph=&quot; &lt;&lt; graph;
  701. std::cout &lt;&lt; std::endl;
  702. std::cout &lt;&lt; &quot;visitor=&quot; &lt;&lt; visitor;
  703. std::cout &lt;&lt; std::endl;
  704. std::cout &lt;&lt; &quot;root_vertex=&quot; &lt;&lt; root_vertex;
  705. std::cout &lt;&lt; std::endl;
  706. std::cout &lt;&lt; &quot;index_map=&quot; &lt;&lt; index_map;
  707. std::cout &lt;&lt; std::endl;
  708. std::cout &lt;&lt; &quot;color_map=&quot; &lt;&lt; color_map;
  709. std::cout &lt;&lt; std::endl;
  710. return true;
  711. }
  712. #include &lt;boost/core/lightweight_test.hpp&gt;
  713. int main()
  714. {
  715. char const* g = &quot;1&quot;;
  716. depth_first_search(1, 2, 3, 4, 5);
  717. depth_first_search(
  718. g, '2', _color_map = '5',
  719. _index_map = &quot;4&quot;, _root_vertex = &quot;3&quot;
  720. );
  721. return boost::report_errors();
  722. }
  723. </pre>
  724. <p class="compound-last">Despite the fact that default expressions such as
  725. <tt class="docutils literal"><span class="pre">vertices(graph).first</span></tt> are ill-formed for the given <tt class="docutils literal">graph</tt>
  726. arguments, both calls will compile, and each one will print exactly the
  727. same thing.</p>
  728. </div>
  729. <!-- @example.prepend('''
  730. #include <boost/parameter.hpp>
  731. #include <iostream>
  732. BOOST_PARAMETER_NAME(graph)
  733. BOOST_PARAMETER_NAME(visitor)
  734. BOOST_PARAMETER_NAME(root_vertex)
  735. BOOST_PARAMETER_NAME(index_map)
  736. BOOST_PARAMETER_NAME(color_map)
  737. ''') -->
  738. <!-- @example.replace_emphasis('''
  739. , (required
  740. (graph, \*)
  741. (visitor, \*)
  742. (root_vertex, \*)
  743. (index_map, \*)
  744. (color_map, \*)
  745. )
  746. ''') -->
  747. <!-- @test('run') -->
  748. </div>
  749. <div class="section" id="signature-matching-and-overloading">
  750. <h4>2.1.5.6&nbsp;&nbsp;&nbsp;Signature Matching and Overloading</h4>
  751. <p>In fact, the function signature is so general that any call to
  752. <tt class="docutils literal">depth_first_search</tt> with fewer than five arguments will match our function,
  753. provided we pass <em>something</em> for the required <tt class="docutils literal">graph</tt> parameter. That might
  754. not seem to be a problem at first; after all, if the arguments don't match the
  755. requirements imposed by the implementation of <tt class="docutils literal">depth_first_search</tt>, a
  756. compilation error will occur later, when its body is instantiated.</p>
  757. <p>There are at least three problems with very general function signatures.</p>
  758. <ol class="arabic simple">
  759. <li>By the time our <tt class="docutils literal">depth_first_search</tt> is instantiated, it has been
  760. selected as the best matching overload. Some other <tt class="docutils literal">depth_first_search</tt>
  761. overload might've worked had it been chosen instead. By the time we see a
  762. compilation error, there's no chance to change that decision.</li>
  763. <li>Even if there are no overloads, error messages generated at instantiation
  764. time usually expose users to confusing implementation details. For
  765. example, users might see references to names generated by
  766. <tt class="docutils literal">BOOST_PARAMETER_FUNCTION</tt> such as
  767. <tt class="docutils literal"><span class="pre">graphs::detail::depth_first_search_with_named_params</span></tt> (or worse—think
  768. of the kinds of errors you get from your STL implementation when you make
  769. a mistake).<a class="footnote-reference" href="#conceptsts" id="id7"><sup>4</sup></a></li>
  770. <li>The problems with exposing such permissive function template signatures
  771. have been the subject of much discussion, especially in the presence of
  772. <a class="reference external" href="http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#225">unqualified calls</a>. If all we want is to avoid unintentional
  773. argument-dependent lookup (ADL), we can isolate <tt class="docutils literal">depth_first_search</tt> in
  774. a namespace containing no types<a class="footnote-reference" href="#using" id="id8"><sup>6</sup></a>, but suppose we <em>want</em> it to
  775. found via ADL?</li>
  776. </ol>
  777. <p>It's usually a good idea to prevent functions from being considered for
  778. overload resolution when the passed argument types aren't appropriate. The
  779. library already does this when the required <tt class="docutils literal">graph</tt> parameter is not
  780. supplied, but we're not likely to see a depth first search that doesn't take a
  781. graph to operate on. Suppose, instead, that we found a different depth first
  782. search algorithm that could work on graphs that don't model
  783. <a class="reference external" href="../../../graph/doc/IncidenceGraph.html"><span class="concept">Incidence Graph</span></a>? If we just added a simple overload, it would be
  784. ambiguous:</p>
  785. <pre class="literal-block">
  786. // new overload
  787. BOOST_PARAMETER_FUNCTION((void), depth_first_search, (tag),
  788. (required (graph,*))( … )
  789. )
  790. {
  791. // new algorithm implementation
  792. }
  793. // ambiguous!
  794. depth_first_search(boost::adjacency_list&lt;&gt;(), 2, &quot;hello&quot;);
  795. </pre>
  796. <!-- @ignore() -->
  797. <div class="section" id="predicate-requirements">
  798. <h5>2.1.5.6.1&nbsp;&nbsp;&nbsp;Predicate Requirements</h5>
  799. <p>We really don't want the compiler to consider the original version of
  800. <tt class="docutils literal">depth_first_search</tt> because the <tt class="docutils literal">root_vertex</tt> argument, <tt class="docutils literal">&quot;hello&quot;</tt>,
  801. doesn't meet the <a class="reference internal" href="#parameter-table">requirement</a> that it match the <tt class="docutils literal">graph</tt> parameter's vertex
  802. descriptor type. Instead, this call should just invoke our new overload. To
  803. take the original <tt class="docutils literal">depth_first_search</tt> overload out of contention, we first
  804. encode this requirement as follows:</p>
  805. <pre class="literal-block">
  806. struct vertex_descriptor_predicate
  807. {
  808. template &lt;typename T, typename Args&gt;
  809. struct apply
  810. : boost::mpl::if_&lt;
  811. boost::is_convertible&lt;
  812. T
  813. , typename boost::graph_traits&lt;
  814. typename boost::parameter::value_type&lt;
  815. Args
  816. , graphs::graph
  817. &gt;::type
  818. &gt;::vertex_descriptor
  819. &gt;
  820. , boost::mpl::true_
  821. , boost::mpl::false_
  822. &gt;
  823. {
  824. };
  825. };
  826. </pre>
  827. <p>This encoding is an <a class="reference external" href="../../../mpl/doc/refmanual/metafunction-class.html">MPL Binary Metafunction Class</a>, a type with a nested
  828. <tt class="docutils literal">apply</tt> metafunction that takes in two template arguments. For the first
  829. template argument, Boost.Parameter will pass in the type on which we will
  830. impose the requirement. For the second template argument, Boost.Parameter
  831. will pass in the entire argument pack, making it possible to extract the
  832. type of each of the other <tt class="docutils literal">depth_first_search</tt> parameters via the
  833. <tt class="docutils literal">value_type</tt> metafunction and the corresponding keyword tag type. The
  834. result <tt class="docutils literal">type</tt> of the <tt class="docutils literal">apply</tt> metafunction will be equivalent to
  835. <tt class="docutils literal"><span class="pre">boost::mpl::true_</span></tt> if <tt class="docutils literal">T</tt> fulfills our requirement as imposed by the
  836. <tt class="docutils literal"><span class="pre">boost::is_convertible</span></tt> statement; otherwise, the result will be
  837. equivalent to <tt class="docutils literal"><span class="pre">boost::mpl::false_</span></tt>.</p>
  838. <p>At this point, we can append the name of our metafunction class, in
  839. parentheses, to the appropriate <tt class="docutils literal">*</tt> element of the function signature.</p>
  840. <pre class="literal-block">
  841. (root_vertex
  842. , *(<strong>vertex_descriptor_predicate</strong>)
  843. , *vertices(graph).first
  844. )
  845. </pre>
  846. <!-- @ignore() -->
  847. <p>Now the original <tt class="docutils literal">depth_first_search</tt> will only be called when the
  848. <tt class="docutils literal">root_vertex</tt> argument can be converted to the graph's vertex descriptor
  849. type, and our example that <em>was</em> ambiguous will smoothly call the new
  850. overload.</p>
  851. <p>We can encode the requirements on other arguments using the same concept; only
  852. the implementation of the nested <tt class="docutils literal">apply</tt> metafunction needs to be tweaked
  853. for each argument. There's no space to give a complete description of graph
  854. library details here, but suffice it to show that the next few metafunction
  855. classes provide the necessary checks.</p>
  856. <pre class="literal-block">
  857. struct graph_predicate
  858. {
  859. template &lt;typename T, typename Args&gt;
  860. struct apply
  861. : boost::mpl::eval_if&lt;
  862. boost::is_convertible&lt;
  863. typename boost::graph_traits&lt;T&gt;::traversal_category
  864. , boost::incidence_graph_tag
  865. &gt;
  866. , boost::mpl::if_&lt;
  867. boost::is_convertible&lt;
  868. typename boost::graph_traits&lt;T&gt;::traversal_category
  869. , boost::vertex_list_graph_tag
  870. &gt;
  871. , boost::mpl::true_
  872. , boost::mpl::false_
  873. &gt;
  874. &gt;
  875. {
  876. };
  877. };
  878. struct index_map_predicate
  879. {
  880. template &lt;typename T, typename Args&gt;
  881. struct apply
  882. : boost::mpl::eval_if&lt;
  883. boost::is_integral&lt;
  884. typename boost::property_traits&lt;T&gt;::value_type
  885. &gt;
  886. , boost::mpl::if_&lt;
  887. boost::is_same&lt;
  888. typename boost::property_traits&lt;T&gt;::key_type
  889. , typename boost::graph_traits&lt;
  890. typename boost::parameter::value_type&lt;
  891. Args
  892. , graphs::graph
  893. &gt;::type
  894. &gt;::vertex_descriptor
  895. &gt;
  896. , boost::mpl::true_
  897. , boost::mpl::false_
  898. &gt;
  899. &gt;
  900. {
  901. };
  902. };
  903. struct color_map_predicate
  904. {
  905. template &lt;typename T, typename Args&gt;
  906. struct apply
  907. : boost::mpl::if_&lt;
  908. boost::is_same&lt;
  909. typename boost::property_traits&lt;T&gt;::key_type
  910. , typename boost::graph_traits&lt;
  911. typename boost::parameter::value_type&lt;
  912. Args
  913. , graphs::graph
  914. &gt;::type
  915. &gt;::vertex_descriptor
  916. &gt;
  917. , boost::mpl::true_
  918. , boost::mpl::false_
  919. &gt;
  920. {
  921. };
  922. };
  923. </pre>
  924. <p>Likewise, computing the default value for the <tt class="docutils literal">color_map</tt> parameter is no
  925. trivial matter, so it's best to factor the computation out to a separate
  926. function template.</p>
  927. <pre class="literal-block">
  928. template &lt;typename Size, typename IndexMap&gt;
  929. boost::iterator_property_map&lt;
  930. std::vector&lt;boost::default_color_type&gt;::iterator
  931. , IndexMap
  932. , boost::default_color_type
  933. , boost::default_color_type&amp;
  934. &gt;&amp;
  935. default_color_map(Size num_vertices, IndexMap const&amp; index_map)
  936. {
  937. static std::vector&lt;boost::default_color_type&gt; colors(num_vertices);
  938. static boost::iterator_property_map&lt;
  939. std::vector&lt;boost::default_color_type&gt;::iterator
  940. , IndexMap
  941. , boost::default_color_type
  942. , boost::default_color_type&amp;
  943. &gt; m(colors.begin(), index_map);
  944. return m;
  945. }
  946. </pre>
  947. <p>The signature encloses each predicate metafunction in parentheses <em>preceded
  948. by an asterix</em>, as follows:</p>
  949. <pre class="literal-block">
  950. BOOST_PARAMETER_FUNCTION((void), depth_first_search, graphs,
  951. (required
  952. (graph, *(<strong>graph_predicate</strong>))
  953. )
  954. (optional
  955. (visitor
  956. , * // not easily checkable
  957. , boost::dfs_visitor&lt;&gt;()
  958. )
  959. (root_vertex
  960. , (<strong>vertex_descriptor_predicate</strong>)
  961. , *vertices(graph).first
  962. )
  963. (index_map
  964. , *(<strong>index_map_predicate</strong>)
  965. , get(boost::vertex_index, graph)
  966. )
  967. (color_map
  968. , *(<strong>color_map_predicate</strong>)
  969. , default_color_map(num_vertices(graph), index_map)
  970. )
  971. )
  972. )
  973. </pre>
  974. <!-- @example.prepend('''
  975. #include <boost/parameter.hpp>
  976. #include <boost/graph/adjacency_list.hpp>
  977. #include <boost/graph/depth_first_search.hpp>
  978. #include <boost/graph/graph_traits.hpp>
  979. #include <boost/property_map/property_map.hpp>
  980. #include <boost/mpl/and.hpp>
  981. #include <boost/type_traits/is_convertible.hpp>
  982. #include <boost/type_traits/is_integral.hpp>
  983. #include <boost/type_traits/is_same.hpp>
  984. #include <vector>
  985. #include <utility>
  986. BOOST_PARAMETER_NAME((_graph, graphs) graph)
  987. BOOST_PARAMETER_NAME((_visitor, graphs) visitor)
  988. BOOST_PARAMETER_NAME((_root_vertex, graphs) in(root_vertex))
  989. BOOST_PARAMETER_NAME((_index_map, graphs) in(index_map))
  990. BOOST_PARAMETER_NAME((_color_map, graphs) in_out(color_map))
  991. ''') -->
  992. <!-- @example.append('''
  993. {
  994. }
  995. #include <boost/core/lightweight_test.hpp>
  996. #include <boost/graph/adjacency_list.hpp>
  997. #include <utility>
  998. int main()
  999. {
  1000. typedef boost::adjacency_list<
  1001. boost::vecS, boost::vecS, boost::directedS
  1002. > G;
  1003. enum {u, v, w, x, y, z, N};
  1004. typedef std::pair<int, int> E;
  1005. E edges[] = {
  1006. E(u, v), E(u, x), E(x, v), E(y, x),
  1007. E(v, y), E(w, y), E(w,z), E(z, z)
  1008. };
  1009. G g(edges, edges + sizeof(edges) / sizeof(E), N);
  1010. depth_first_search(g);
  1011. depth_first_search(g, _root_vertex = static_cast<int>(x));
  1012. return boost::report_errors();
  1013. }
  1014. ''') -->
  1015. <!-- @test('run') -->
  1016. <p>It usually isn't necessary to so completely encode the type requirements on
  1017. arguments to generic functions. However, doing so is worth the effort: your
  1018. code will be more self-documenting and will often provide a better user
  1019. experience. You'll also have an easier transition to the C++20 standard with
  1020. <a class="reference internal" href="#conceptsts">language support for constraints and concepts</a>.</p>
  1021. </div>
  1022. <div class="section" id="more-on-type-requirements">
  1023. <h5>2.1.5.6.2&nbsp;&nbsp;&nbsp;More on Type Requirements</h5>
  1024. <p>Encoding type requirements onto a function's parameters is essential for
  1025. enabling the function to have deduced parameter interface. Let's revisit the
  1026. <tt class="docutils literal">new_window</tt> example for a moment:</p>
  1027. <pre class="literal-block">
  1028. window* w = new_window(
  1029. movable_=false
  1030. , &quot;alert box&quot;
  1031. );
  1032. window* w = new_window(
  1033. &quot;alert box&quot;
  1034. , movable_=false
  1035. );
  1036. </pre>
  1037. <!-- @ignore() -->
  1038. <p>The goal this time is to be able to invoke the <tt class="docutils literal">new_window</tt> function without
  1039. specifying the keywords. For each parameter that has a required type, we can
  1040. enclose that type in parentheses, then <em>replace</em> the <tt class="docutils literal">*</tt> element of the
  1041. parameter signature:</p>
  1042. <pre class="literal-block">
  1043. BOOST_PARAMETER_NAME((name_, keywords) name)
  1044. BOOST_PARAMETER_NAME((movable_, keywords) movable)
  1045. BOOST_PARAMETER_FUNCTION((window*), new_window, keywords,
  1046. (deduced
  1047. (required
  1048. (name, <em>(char const*)</em>)
  1049. (movable, <em>(bool)</em>)
  1050. )
  1051. )
  1052. )
  1053. {
  1054. // ...
  1055. }
  1056. </pre>
  1057. <!-- @ignore() -->
  1058. <p>The following statements will now work and are equivalent to each other as
  1059. well as the previous statements:</p>
  1060. <pre class="literal-block">
  1061. window* w = new_window(false, &quot;alert box&quot;);
  1062. window* w = new_window(&quot;alert box&quot;, false);
  1063. </pre>
  1064. <!-- @ignore() -->
  1065. </div>
  1066. </div>
  1067. <div class="section" id="deduced-parameters">
  1068. <h4>2.1.5.7&nbsp;&nbsp;&nbsp;Deduced Parameters</h4>
  1069. <p>To further illustrate deduced parameter support, consider the example of the
  1070. <a class="reference external" href="../../../python/doc/v2/def.html"><tt class="docutils literal">def</tt></a> function from <a class="reference external" href="../../../python/doc/index.html">Boost.Python</a>. Its signature is roughly as follows:</p>
  1071. <pre class="literal-block">
  1072. template &lt;
  1073. typename Function
  1074. , typename KeywordExpression
  1075. , typename CallPolicies
  1076. &gt;
  1077. void def(
  1078. // Required parameters
  1079. char const* name, Function func
  1080. // Optional, deduced parameters
  1081. , char const* docstring = &quot;&quot;
  1082. , KeywordExpression keywords = no_keywords()
  1083. , CallPolicies policies = default_call_policies()
  1084. );
  1085. </pre>
  1086. <!-- @ignore() -->
  1087. <p>Try not to be too distracted by the use of the term “keywords” in this
  1088. example: although it means something analogous in Boost.Python to what
  1089. it means in the Parameter library, for the purposes of this exercise
  1090. you can think of it as being completely different.</p>
  1091. <p>When calling <tt class="docutils literal">def</tt>, only two arguments are required. The association
  1092. between any additional arguments and their parameters can be determined by the
  1093. types of the arguments actually passed, so the caller is neither required to
  1094. remember argument positions or explicitly specify parameter names for those
  1095. arguments. To generate this interface using <tt class="docutils literal">BOOST_PARAMETER_FUNCTION</tt>, we
  1096. need only enclose the deduced parameters in a <tt class="docutils literal">(deduced …)</tt> clause, as
  1097. follows:</p>
  1098. <pre class="literal-block">
  1099. char const*&amp; blank_char_ptr()
  1100. {
  1101. static char const* larr = &quot;&quot;;
  1102. return larr;
  1103. }
  1104. BOOST_PARAMETER_FUNCTION(
  1105. (bool), def, tag,
  1106. (required (name, (char const*)) (func,*) ) // nondeduced
  1107. <strong>(deduced</strong>
  1108. (optional
  1109. (docstring, (char const*), &quot;&quot;)
  1110. (keywords
  1111. // see<a class="footnote-reference" href="#is-keyword-expression" id="id13"><sup>5</sup></a>
  1112. , *(is_keyword_expression&lt;boost::mpl::_&gt;)
  1113. , no_keywords()
  1114. )
  1115. (policies
  1116. , *(
  1117. boost::mpl::eval_if&lt;
  1118. boost::is_convertible&lt;boost::mpl::_,char const*&gt;
  1119. , boost::mpl::false_
  1120. , boost::mpl::if_&lt;
  1121. // see<a class="footnote-reference" href="#is-keyword-expression" id="id14"><sup>5</sup></a>
  1122. is_keyword_expression&lt;boost::mpl::_&gt;
  1123. , boost::mpl::false_
  1124. , boost::mpl::true_
  1125. &gt;
  1126. &gt;
  1127. )
  1128. , default_call_policies()
  1129. )
  1130. )
  1131. <strong>)</strong>
  1132. )
  1133. {
  1134. <em>…</em>
  1135. }
  1136. </pre>
  1137. <!-- @example.replace_emphasis('return true;') -->
  1138. <!-- @example.prepend('''
  1139. #include <boost/parameter.hpp>
  1140. BOOST_PARAMETER_NAME(name)
  1141. BOOST_PARAMETER_NAME(func)
  1142. BOOST_PARAMETER_NAME(docstring)
  1143. BOOST_PARAMETER_NAME(keywords)
  1144. BOOST_PARAMETER_NAME(policies)
  1145. struct default_call_policies
  1146. {
  1147. };
  1148. struct no_keywords
  1149. {
  1150. };
  1151. struct keywords
  1152. {
  1153. };
  1154. template <typename T>
  1155. struct is_keyword_expression
  1156. : boost::mpl::false_
  1157. {
  1158. };
  1159. template <>
  1160. struct is_keyword_expression<keywords>
  1161. : boost::mpl::true_
  1162. {
  1163. };
  1164. default_call_policies some_policies;
  1165. void f()
  1166. {
  1167. }
  1168. #include <boost/mpl/placeholders.hpp>
  1169. #include <boost/mpl/if.hpp>
  1170. #include <boost/mpl/eval_if.hpp>
  1171. #include <boost/type_traits/is_convertible.hpp>
  1172. ''') -->
  1173. <div class="admonition-syntax-note admonition">
  1174. <p class="first admonition-title">Syntax Note</p>
  1175. <p class="last">A <tt class="docutils literal">(deduced …)</tt> clause always contains a <tt class="docutils literal">(required …)</tt> and/or an
  1176. <tt class="docutils literal">(optional …)</tt> subclause, and must follow any <tt class="docutils literal">(required …)</tt> or
  1177. <tt class="docutils literal">(optional …)</tt> clauses indicating nondeduced parameters at the outer
  1178. level.</p>
  1179. </div>
  1180. <p>With the declaration above, the following two calls are equivalent:</p>
  1181. <pre class="literal-block">
  1182. char const* f_name = &quot;f&quot;;
  1183. def(
  1184. f_name
  1185. , &amp;f
  1186. , <strong>some_policies</strong>
  1187. , <strong>&quot;Documentation for f&quot;</strong>
  1188. );
  1189. def(
  1190. f_name
  1191. , &amp;f
  1192. , <strong>&quot;Documentation for f&quot;</strong>
  1193. , <strong>some_policies</strong>
  1194. );
  1195. </pre>
  1196. <!-- @example.prepend('''
  1197. int main()
  1198. {
  1199. ''') -->
  1200. <p>If the user wants to pass a <tt class="docutils literal">policies</tt> argument that was also, for some
  1201. reason, convertible to <tt class="docutils literal">char const*</tt>, she can always specify the parameter
  1202. name explicitly, as follows:</p>
  1203. <pre class="literal-block">
  1204. def(
  1205. f_name
  1206. , &amp;f
  1207. , <strong>_policies = some_policies</strong>
  1208. , &quot;Documentation for f&quot;
  1209. );
  1210. </pre>
  1211. <!-- @example.append('}') -->
  1212. <!-- @test('compile', howmany='all') -->
  1213. <p>The <a class="reference external" href="../../test/deduced.cpp">deduced.cpp</a> and <a class="reference external" href="../../test/deduced_dependent_predicate.cpp">deduced_dependent_predicate.cpp</a> test programs
  1214. demonstrate additional usage of deduced parameter support.</p>
  1215. </div>
  1216. <div class="section" id="parameter-dependent-return-types">
  1217. <h4>2.1.5.8&nbsp;&nbsp;&nbsp;Parameter-Dependent Return Types</h4>
  1218. <p>For some algorithms, the return type depends on at least one of the argument
  1219. types. However, there is one gotcha to avoid when encoding this return type
  1220. while using <tt class="docutils literal">BOOST_PARAMETER_FUNCTION</tt> or other code generation macros. As
  1221. an example, given the following definitions:</p>
  1222. <pre class="literal-block">
  1223. BOOST_PARAMETER_NAME(x)
  1224. BOOST_PARAMETER_NAME(y)
  1225. BOOST_PARAMETER_NAME(z)
  1226. </pre>
  1227. <!-- @ignore() -->
  1228. <p>Let our algorithm simply return one of its arguments. If we naïvely implement
  1229. its return type in terms of <tt class="docutils literal"><span class="pre">parameter::value_type</span></tt>:</p>
  1230. <pre class="literal-block">
  1231. BOOST_PARAMETER_FUNCTION(
  1232. (typename parameter::value_type&lt;Args,tag::y&gt;::type), return_y, tag,
  1233. (deduced
  1234. (required
  1235. (x, (std::map&lt;char const*,std::string&gt;))
  1236. (y, (char const*))
  1237. )
  1238. (optional
  1239. (z, (int), 4)
  1240. )
  1241. )
  1242. )
  1243. {
  1244. return y;
  1245. }
  1246. </pre>
  1247. <!-- @ignore() -->
  1248. <p>Then using <tt class="docutils literal">return_y</tt> in any manner other than with positional arguments
  1249. will result in a compiler error:</p>
  1250. <pre class="literal-block">
  1251. std::map&lt;char const*,std::string&gt; k2s;
  1252. assert(&quot;foo&quot; == return_y(2, k2s, &quot;foo&quot;)); // error!
  1253. </pre>
  1254. <!-- @ignore() -->
  1255. <p>The problem is that even though <tt class="docutils literal">y</tt> is a required parameter,
  1256. <tt class="docutils literal">BOOST_PARAMETER_FUNCTION</tt> will generate certain overloads for which the
  1257. argument pack type <tt class="docutils literal">Args</tt> does not actually contain the keyword tag type
  1258. <tt class="docutils literal"><span class="pre">tag::y</span></tt>. The solution is to use SFINAE to preclude generation of those
  1259. overloads. Since <tt class="docutils literal"><span class="pre">parameter::value_type</span></tt> is a metafunction, our tool for
  1260. the job is <tt class="docutils literal">lazy_enable_if</tt>:</p>
  1261. <pre class="literal-block">
  1262. BOOST_PARAMETER_FUNCTION(
  1263. (
  1264. // Whenever using 'enable_if', 'disable_if', and so on,
  1265. // do not add the 'typename' keyword in front.
  1266. boost::lazy_enable_if&lt;
  1267. typename mpl::has_key&lt;Args,tag::y&gt;::type
  1268. , parameter::value_type&lt;Args,tag::y&gt;
  1269. &gt;
  1270. // Whenever using 'enable_if', 'disable_if', and so on,
  1271. // do not add '::type' here.
  1272. ), return_y, tag,
  1273. (deduced
  1274. (required
  1275. (x, (std::map&lt;char const*,std::string&gt;))
  1276. (y, (char const*))
  1277. )
  1278. (optional
  1279. (z, (int), 4)
  1280. )
  1281. )
  1282. )
  1283. {
  1284. return y;
  1285. }
  1286. </pre>
  1287. <!-- @ignore() -->
  1288. <p>For a working demonstration, see <a class="reference external" href="../../test/preprocessor_deduced.cpp">preprocessor_deduced.cpp</a>.</p>
  1289. </div>
  1290. </div>
  1291. </div>
  1292. <div class="section" id="parameter-enabled-member-functions">
  1293. <h2><a class="toc-backref" href="#id28">2.2&nbsp;&nbsp;&nbsp;Parameter-Enabled Member Functions</a></h2>
  1294. <p>The <tt class="docutils literal">BOOST_PARAMETER_MEMBER_FUNCTION</tt> and
  1295. <tt class="docutils literal">BOOST_PARAMETER_CONST_MEMBER_FUNCTION</tt> macros accept exactly the same
  1296. arguments as <tt class="docutils literal">BOOST_PARAMETER_FUNCTION</tt>, but are designed to be used within
  1297. the body of a class:</p>
  1298. <pre class="literal-block">
  1299. BOOST_PARAMETER_NAME(arg1)
  1300. BOOST_PARAMETER_NAME(arg2)
  1301. struct callable2
  1302. {
  1303. BOOST_PARAMETER_CONST_MEMBER_FUNCTION(
  1304. (void), call, tag, (required (arg1,(int))(arg2,(int)))
  1305. )
  1306. {
  1307. std::cout &lt;&lt; arg1 &lt;&lt; &quot;, &quot; &lt;&lt; arg2;
  1308. std::cout &lt;&lt; std::endl;
  1309. }
  1310. };
  1311. #include &lt;boost/core/lightweight_test.hpp&gt;
  1312. int main()
  1313. {
  1314. callable2 c2;
  1315. callable2 const&amp; c2_const = c2;
  1316. c2_const.call(1, 2);
  1317. return boost::report_errors();
  1318. }
  1319. </pre>
  1320. <!-- @example.prepend('''
  1321. #include <boost/parameter.hpp>
  1322. #include <iostream>
  1323. using namespace boost::parameter;
  1324. ''') -->
  1325. <!-- @test('run') -->
  1326. <p>These macros don't directly allow a function's interface to be separated from
  1327. its implementation, but you can always forward arguments on to a separate
  1328. implementation function:</p>
  1329. <pre class="literal-block">
  1330. struct callable2
  1331. {
  1332. BOOST_PARAMETER_CONST_MEMBER_FUNCTION(
  1333. (void), call, tag, (required (arg1,(int))(arg2,(int)))
  1334. )
  1335. {
  1336. call_impl(arg1, arg2);
  1337. }
  1338. private:
  1339. void call_impl(int, int); // implemented elsewhere.
  1340. };
  1341. </pre>
  1342. <!-- @example.prepend('''
  1343. #include <boost/parameter.hpp>
  1344. BOOST_PARAMETER_NAME(arg1)
  1345. BOOST_PARAMETER_NAME(arg2)
  1346. using namespace boost::parameter;
  1347. ''') -->
  1348. <!-- @test('compile') -->
  1349. <div class="section" id="static-member-functions">
  1350. <h3>2.2.1&nbsp;&nbsp;&nbsp;Static Member Functions</h3>
  1351. <p>To expose a static member function, simply insert the keyword “<tt class="docutils literal">static</tt>”
  1352. before the function name:</p>
  1353. <pre class="literal-block">
  1354. BOOST_PARAMETER_NAME(arg1)
  1355. struct somebody
  1356. {
  1357. BOOST_PARAMETER_MEMBER_FUNCTION(
  1358. (void), <strong>static</strong> f, tag, (optional (arg1,(int),0))
  1359. )
  1360. {
  1361. std::cout &lt;&lt; arg1 &lt;&lt; std::endl;
  1362. }
  1363. };
  1364. #include &lt;boost/core/lightweight_test.hpp&gt;
  1365. int main()
  1366. {
  1367. somebody::f();
  1368. somebody::f(4);
  1369. return boost::report_errors();
  1370. }
  1371. </pre>
  1372. <!-- @example.prepend('''
  1373. #include <boost/parameter.hpp>
  1374. #include <iostream>
  1375. using namespace boost::parameter;
  1376. ''') -->
  1377. <!-- @test('run') -->
  1378. </div>
  1379. </div>
  1380. <div class="section" id="parameter-enabled-function-call-operators">
  1381. <h2><a class="toc-backref" href="#id29">2.3&nbsp;&nbsp;&nbsp;Parameter-Enabled Function Call Operators</a></h2>
  1382. <p>The <tt class="docutils literal">BOOST_PARAMETER_FUNCTION_CALL_OPERATOR</tt> and
  1383. <tt class="docutils literal">BOOST_PARAMETER_CONST_FUNCTION_CALL_OPERATOR</tt> macros accept the same
  1384. arguments as the <tt class="docutils literal">BOOST_PARAMETER_MEMBER_FUNCTION</tt> and
  1385. <tt class="docutils literal">BOOST_PARAMETER_CONST_MEMBER_FUNCTION</tt> macros except for the function name,
  1386. because these macros allow instances of the enclosing classes to be treated as
  1387. function objects:</p>
  1388. <pre class="literal-block">
  1389. BOOST_PARAMETER_NAME(first_arg)
  1390. BOOST_PARAMETER_NAME(second_arg)
  1391. struct callable2
  1392. {
  1393. BOOST_PARAMETER_CONST_FUNCTION_CALL_OPERATOR(
  1394. (void), tag, (required (first_arg,(int))(second_arg,(int)))
  1395. )
  1396. {
  1397. std::cout &lt;&lt; first_arg &lt;&lt; &quot;, &quot;;
  1398. std::cout &lt;&lt; second_arg &lt;&lt; std::endl;
  1399. }
  1400. };
  1401. #include &lt;boost/core/lightweight_test.hpp&gt;
  1402. int main()
  1403. {
  1404. callable2 c2;
  1405. callable2 const&amp; c2_const = c2;
  1406. c2_const(1, 2);
  1407. return boost::report_errors();
  1408. }
  1409. </pre>
  1410. <!-- @example.prepend('''
  1411. #include <boost/parameter.hpp>
  1412. #include <iostream>
  1413. using namespace boost::parameter;
  1414. ''') -->
  1415. <!-- @test('run') -->
  1416. </div>
  1417. <div class="section" id="parameter-enabled-constructors">
  1418. <h2><a class="toc-backref" href="#id30">2.4&nbsp;&nbsp;&nbsp;Parameter-Enabled Constructors</a></h2>
  1419. <p>The lack of a “delegating constructor” feature in C++
  1420. (<a class="reference external" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1986.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1986.pdf</a>)
  1421. limits somewhat the quality of interface this library can provide
  1422. for defining parameter-enabled constructors. The usual workaround
  1423. for a lack of constructor delegation applies: one must factor the
  1424. common logic into one or more base classes.</p>
  1425. <p>Let's build a parameter-enabled constructor that simply prints its
  1426. arguments. The first step is to write a base class whose
  1427. constructor accepts a single argument known as an <a class="reference external" href="reference.html#argumentpack"><span class="concept">ArgumentPack</span></a>:
  1428. a bundle of references to the actual arguments, tagged with their
  1429. keywords. The values of the actual arguments are extracted from
  1430. the <span class="concept">ArgumentPack</span> by <em>indexing</em> it with keyword objects:</p>
  1431. <pre class="literal-block">
  1432. BOOST_PARAMETER_NAME(name)
  1433. BOOST_PARAMETER_NAME(index)
  1434. struct myclass_impl
  1435. {
  1436. template &lt;typename ArgumentPack&gt;
  1437. myclass_impl(ArgumentPack const&amp; args)
  1438. {
  1439. std::cout &lt;&lt; &quot;name = &quot; &lt;&lt; args[_name];
  1440. std::cout &lt;&lt; &quot;; index = &quot; &lt;&lt; args[_index | 42];
  1441. std::cout &lt;&lt; std::endl;
  1442. }
  1443. };
  1444. </pre>
  1445. <!-- @example.prepend('''
  1446. #include <boost/parameter.hpp>
  1447. #include <iostream>
  1448. ''') -->
  1449. <p>Note that the bitwise or (“<tt class="docutils literal">|</tt>”) operator has a special meaning when
  1450. applied to keyword objects that are passed to an <span class="concept">ArgumentPack</span>'s indexing
  1451. operator: it is used to indicate a default value. In this case if there is no
  1452. <tt class="docutils literal">index</tt> parameter in the <span class="concept">ArgumentPack</span>, <tt class="docutils literal">42</tt> will be used instead.</p>
  1453. <p>Now we are ready to write the parameter-enabled constructor interface:</p>
  1454. <pre class="literal-block">
  1455. struct myclass : myclass_impl
  1456. {
  1457. BOOST_PARAMETER_CONSTRUCTOR(
  1458. myclass, (myclass_impl), tag
  1459. , (required (name,*)) (optional (index,*))
  1460. ) // no semicolon
  1461. };
  1462. </pre>
  1463. <p>Since we have supplied a default value for <tt class="docutils literal">index</tt> but not for <tt class="docutils literal">name</tt>,
  1464. only <tt class="docutils literal">name</tt> is required. We can exercise our new interface as follows:</p>
  1465. <pre class="literal-block">
  1466. myclass x(&quot;bob&quot;, 3); // positional
  1467. myclass y(_index = 12, _name = &quot;sally&quot;); // named
  1468. myclass z(&quot;june&quot;); // positional/defaulted
  1469. </pre>
  1470. <!-- @example.wrap('''
  1471. #include <boost/core/lightweight_test.hpp>
  1472. int main() {
  1473. ''', ' return boost::report_errors(); }') -->
  1474. <!-- @test('run', howmany='all') -->
  1475. <p>For more on <span class="concept">ArgumentPack</span> manipulation, see the <a class="reference internal" href="#advanced-topics">Advanced Topics</a> section.</p>
  1476. </div>
  1477. <div class="section" id="parameter-enabled-class-templates">
  1478. <h2><a class="toc-backref" href="#id31">2.5&nbsp;&nbsp;&nbsp;Parameter-Enabled Class Templates</a></h2>
  1479. <p>In this section we'll use Boost.Parameter to build <a class="reference external" href="../../../python/doc/index.html">Boost.Python</a>'s <a class="reference external" href="http://www.boost.org/libs/python/doc/v2/class.html#class_-spec">class_</a> template, whose “signature” is:</p>
  1480. <pre class="literal-block">
  1481. template &lt;
  1482. ValueType, BaseList = bases&lt;&gt;
  1483. , HeldType = ValueType, Copyable = void
  1484. &gt;
  1485. class class_;
  1486. </pre>
  1487. <!-- @ignore() -->
  1488. <p>Only the first argument, <tt class="docutils literal">ValueType</tt>, is required.</p>
  1489. <div class="section" id="named-template-parameters">
  1490. <h3>2.5.1&nbsp;&nbsp;&nbsp;Named Template Parameters</h3>
  1491. <p>First, we'll build an interface that allows users to pass arguments
  1492. positionally or by name:</p>
  1493. <pre class="literal-block">
  1494. struct B
  1495. {
  1496. virtual ~B() = 0;
  1497. };
  1498. struct D : B
  1499. {
  1500. ~D();
  1501. };
  1502. class_&lt;
  1503. <strong>class_type&lt;B&gt;</strong>
  1504. , <strong>copyable&lt;boost::noncopyable&gt;</strong>
  1505. &gt; …;
  1506. class_&lt;
  1507. <strong>D</strong>
  1508. , <strong>held_type&lt;std::auto_ptr&lt;D&gt; &gt;</strong>
  1509. , <strong>base_list&lt;bases&lt;B&gt; &gt;</strong>
  1510. &gt; …;
  1511. </pre>
  1512. <!-- @ignore() -->
  1513. <div class="section" id="template-keywords">
  1514. <h4>2.5.1.1&nbsp;&nbsp;&nbsp;Template Keywords</h4>
  1515. <p>The first step is to define keywords for each template parameter:</p>
  1516. <pre class="literal-block">
  1517. namespace boost { namespace python {
  1518. BOOST_PARAMETER_TEMPLATE_KEYWORD(class_type)
  1519. BOOST_PARAMETER_TEMPLATE_KEYWORD(base_list)
  1520. BOOST_PARAMETER_TEMPLATE_KEYWORD(held_type)
  1521. BOOST_PARAMETER_TEMPLATE_KEYWORD(copyable)
  1522. }}
  1523. </pre>
  1524. <!-- @example.prepend('#include <boost/parameter.hpp>') -->
  1525. <!-- @test('compile') -->
  1526. <p>The declaration of the <tt class="docutils literal">class_type</tt> keyword you see here is equivalent to:</p>
  1527. <pre class="literal-block">
  1528. namespace boost { namespace python {
  1529. namespace tag {
  1530. struct class_type; // keyword tag type
  1531. }
  1532. template &lt;typename T&gt;
  1533. struct class_type
  1534. : parameter::template_keyword&lt;tag::class_type,T&gt;
  1535. {
  1536. };
  1537. }}
  1538. </pre>
  1539. <!-- @example.prepend('#include <boost/parameter.hpp>') -->
  1540. <!-- @test('compile') -->
  1541. <p>It defines a keyword tag type named <tt class="docutils literal"><span class="pre">tag::class_type</span></tt> and a
  1542. <em>parameter passing template</em> named <tt class="docutils literal">class_type</tt>.</p>
  1543. </div>
  1544. <div class="section" id="class-template-skeleton">
  1545. <h4>2.5.1.2&nbsp;&nbsp;&nbsp;Class Template Skeleton</h4>
  1546. <p>The next step is to define the skeleton of our class template, which has three
  1547. optional parameters. Because the user may pass arguments in any order, we
  1548. don't know the actual identities of these parameters, so it would be premature
  1549. to use descriptive names or write out the actual default values for any of
  1550. them. Instead, we'll give them generic names and use the special type
  1551. <tt class="docutils literal"><span class="pre">boost::parameter::void_</span></tt> as a default:</p>
  1552. <pre class="literal-block">
  1553. namespace boost { namespace python {
  1554. template &lt;
  1555. typename A0
  1556. , typename A1 = boost::parameter::void_
  1557. , typename A2 = boost::parameter::void_
  1558. , typename A3 = boost::parameter::void_
  1559. &gt;
  1560. struct class_
  1561. {
  1562. <em>…</em>
  1563. };
  1564. }}
  1565. </pre>
  1566. <!-- @example.prepend('#include <boost/parameter.hpp>') -->
  1567. <!-- @example.replace_emphasis('') -->
  1568. <!-- @test('compile') -->
  1569. </div>
  1570. <div class="section" id="class-template-signatures">
  1571. <h4>2.5.1.3&nbsp;&nbsp;&nbsp;Class Template Signatures</h4>
  1572. <p>Next, we need to build a type, known as a <a class="reference external" href="reference.html#parameterspec"><span class="concept">ParameterSpec</span></a>, describing the
  1573. “signature” of <tt class="docutils literal"><span class="pre">boost::python::class_</span></tt>. A <a class="reference external" href="reference.html#parameterspec"><span class="concept">ParameterSpec</span></a> enumerates the
  1574. required and optional parameters in their positional order, along with any
  1575. type requirements (note that it does <em>not</em> specify defaults -- those will be
  1576. dealt with separately):</p>
  1577. <pre class="literal-block">
  1578. namespace boost { namespace python {
  1579. using boost::mpl::_;
  1580. typedef parameter::parameters&lt;
  1581. required&lt;tag::class_type, boost::is_class&lt;_&gt; &gt;
  1582. , parameter::optional&lt;tag::base_list, mpl::is_sequence&lt;_&gt; &gt;
  1583. , parameter::optional&lt;tag::held_type&gt;
  1584. , parameter::optional&lt;tag::copyable&gt;
  1585. &gt; class_signature;
  1586. }}
  1587. </pre>
  1588. <!-- @example.prepend('''
  1589. #include <boost/parameter.hpp>
  1590. #include <boost/mpl/is_sequence.hpp>
  1591. #include <boost/noncopyable.hpp>
  1592. #include <boost/type_traits/is_class.hpp>
  1593. #include <memory>
  1594. using namespace boost::parameter;
  1595. namespace boost { namespace python {
  1596. BOOST_PARAMETER_TEMPLATE_KEYWORD(class_type)
  1597. BOOST_PARAMETER_TEMPLATE_KEYWORD(base_list)
  1598. BOOST_PARAMETER_TEMPLATE_KEYWORD(held_type)
  1599. BOOST_PARAMETER_TEMPLATE_KEYWORD(copyable)
  1600. template <typename B = int>
  1601. struct bases
  1602. {
  1603. };
  1604. }}
  1605. ''') -->
  1606. </div>
  1607. <div class="section" id="argument-packs-and-parameter-extraction">
  1608. <span id="binding-intro"></span><h4>2.5.1.4&nbsp;&nbsp;&nbsp;Argument Packs and Parameter Extraction</h4>
  1609. <p>Next, within the body of <tt class="docutils literal">class_</tt> , we use the <span class="concept">ParameterSpec</span>'s
  1610. nested <tt class="docutils literal">::bind&lt; … &gt;</tt> template to bundle the actual arguments into an
  1611. <a class="reference external" href="reference.html#argumentpack"><span class="concept">ArgumentPack</span></a> type, and then use the library's <tt class="docutils literal">value_type&lt; … &gt;</tt>
  1612. metafunction to extract “logical parameters”. <tt class="docutils literal">value_type&lt; … &gt;</tt> is
  1613. a lot like <tt class="docutils literal">binding&lt; … &gt;</tt>, but no reference is added to the actual
  1614. argument type. Note that defaults are specified by passing it an
  1615. optional third argument:</p>
  1616. <pre class="literal-block">
  1617. namespace boost { namespace python {
  1618. template &lt;
  1619. typename A0
  1620. , typename A1 = boost::parameter::void_
  1621. , typename A2 = boost::parameter::void_
  1622. , typename A3 = boost::parameter::void_
  1623. &gt;
  1624. struct class_
  1625. {
  1626. // Create ArgumentPack
  1627. typedef typename class_signature::template bind&lt;
  1628. A0, A1, A2, A3
  1629. &gt;::type args;
  1630. // Extract first logical parameter.
  1631. typedef typename parameter::value_type&lt;
  1632. args, tag::class_type
  1633. &gt;::type class_type;
  1634. typedef typename parameter::value_type&lt;
  1635. args, tag::base_list, bases&lt;&gt;
  1636. &gt;::type base_list;
  1637. typedef typename parameter::value_type&lt;
  1638. args, tag::held_type, class_type
  1639. &gt;::type held_type;
  1640. typedef typename parameter::value_type&lt;
  1641. args, tag::copyable, void
  1642. &gt;::type copyable;
  1643. };
  1644. }}
  1645. </pre>
  1646. </div>
  1647. </div>
  1648. <div class="section" id="exercising-the-code-so-far">
  1649. <h3>2.5.2&nbsp;&nbsp;&nbsp;Exercising the Code So Far</h3>
  1650. <div class="compound">
  1651. <p class="compound-first">Revisiting our original examples,</p>
  1652. <pre class="compound-middle literal-block">
  1653. typedef boost::python::class_&lt;
  1654. class_type&lt;B&gt;, copyable&lt;boost::noncopyable&gt;
  1655. &gt; c1;
  1656. typedef boost::python::class_&lt;
  1657. D
  1658. , held_type&lt;std::auto_ptr&lt;D&gt; &gt;
  1659. , base_list&lt;bases&lt;B&gt; &gt;
  1660. &gt; c2;
  1661. </pre>
  1662. <!-- @example.prepend('''
  1663. using boost::python::class_type;
  1664. using boost::python::copyable;
  1665. using boost::python::held_type;
  1666. using boost::python::base_list;
  1667. using boost::python::bases;
  1668. struct B
  1669. {
  1670. };
  1671. struct D
  1672. {
  1673. };
  1674. ''') -->
  1675. <p class="compound-middle">we can now examine the intended parameters:</p>
  1676. <pre class="compound-last literal-block">
  1677. BOOST_MPL_ASSERT((boost::is_same&lt;c1::class_type, B&gt;));
  1678. BOOST_MPL_ASSERT((boost::is_same&lt;c1::base_list, bases&lt;&gt; &gt;));
  1679. BOOST_MPL_ASSERT((boost::is_same&lt;c1::held_type, B&gt;));
  1680. BOOST_MPL_ASSERT((
  1681. boost::is_same&lt;c1::copyable, boost::noncopyable&gt;
  1682. ));
  1683. BOOST_MPL_ASSERT((boost::is_same&lt;c2::class_type, D&gt;));
  1684. BOOST_MPL_ASSERT((boost::is_same&lt;c2::base_list, bases&lt;B&gt; &gt;));
  1685. BOOST_MPL_ASSERT((
  1686. boost::is_same&lt;c2::held_type, std::auto_ptr&lt;D&gt; &gt;
  1687. ));
  1688. BOOST_MPL_ASSERT((boost::is_same&lt;c2::copyable, void&gt;));
  1689. </pre>
  1690. </div>
  1691. <!-- @test('compile', howmany='all') -->
  1692. </div>
  1693. <div class="section" id="deduced-template-parameters">
  1694. <h3>2.5.3&nbsp;&nbsp;&nbsp;Deduced Template Parameters</h3>
  1695. <p>To apply a deduced parameter interface here, we need only make the type
  1696. requirements a bit tighter so the <tt class="docutils literal">held_type</tt> and <tt class="docutils literal">copyable</tt> parameters
  1697. can be crisply distinguished from the others. <a class="reference external" href="../../../python/doc/index.html">Boost.Python</a> does this by
  1698. requiring that <tt class="docutils literal">base_list</tt> be a specialization of its <tt class="docutils literal">bases&lt; … &gt;</tt>
  1699. template (as opposed to being any old MPL sequence) and by requiring that
  1700. <tt class="docutils literal">copyable</tt>, if explicitly supplied, be <tt class="docutils literal"><span class="pre">boost::noncopyable</span></tt>. One easy way
  1701. of identifying specializations of <tt class="docutils literal">bases&lt; … &gt;</tt> is to derive them all from
  1702. the same class, as an implementation detail:</p>
  1703. <pre class="literal-block">
  1704. namespace boost { namespace python {
  1705. namespace detail {
  1706. struct bases_base
  1707. {
  1708. };
  1709. }
  1710. template &lt;
  1711. typename A0 = void, typename A1 = void, typename A2 = void <em>…</em>
  1712. &gt;
  1713. struct bases <strong>: detail::bases_base</strong>
  1714. {
  1715. };
  1716. }}
  1717. </pre>
  1718. <!-- @example.replace_emphasis('') -->
  1719. <!-- @example.prepend('''
  1720. #include <boost/parameter.hpp>
  1721. #include <boost/mpl/is_sequence.hpp>
  1722. #include <boost/noncopyable.hpp>
  1723. #include <memory>
  1724. using namespace boost::parameter;
  1725. using boost::mpl::_;
  1726. namespace boost { namespace python {
  1727. BOOST_PARAMETER_TEMPLATE_KEYWORD(class_type)
  1728. BOOST_PARAMETER_TEMPLATE_KEYWORD(base_list)
  1729. BOOST_PARAMETER_TEMPLATE_KEYWORD(held_type)
  1730. BOOST_PARAMETER_TEMPLATE_KEYWORD(copyable)
  1731. }}
  1732. ''') -->
  1733. <p>Now we can rewrite our signature to make all three optional parameters
  1734. deducible:</p>
  1735. <pre class="literal-block">
  1736. typedef parameter::parameters&lt;
  1737. required&lt;tag::class_type, is_class&lt;_&gt; &gt;
  1738. , parameter::optional&lt;
  1739. deduced&lt;tag::base_list&gt;
  1740. , is_base_and_derived&lt;detail::bases_base,_&gt;
  1741. &gt;
  1742. , parameter::optional&lt;
  1743. deduced&lt;tag::held_type&gt;
  1744. , mpl::not_&lt;
  1745. mpl::or_&lt;
  1746. is_base_and_derived&lt;detail::bases_base,_&gt;
  1747. , is_same&lt;noncopyable,_&gt;
  1748. &gt;
  1749. &gt;
  1750. &gt;
  1751. , parameter::optional&lt;
  1752. deduced&lt;tag::copyable&gt;
  1753. , is_same&lt;noncopyable,_&gt;
  1754. &gt;
  1755. &gt; class_signature;
  1756. </pre>
  1757. <!-- @example.prepend('''
  1758. #include <boost/type_traits/is_class.hpp>
  1759. namespace boost { namespace python {
  1760. ''') -->
  1761. <!-- @example.append('''
  1762. template <
  1763. typename A0
  1764. , typename A1 = boost::parameter::void_
  1765. , typename A2 = boost::parameter::void_
  1766. , typename A3 = boost::parameter::void_
  1767. >
  1768. struct class_
  1769. {
  1770. // Create ArgumentPack
  1771. typedef typename class_signature::bind<
  1772. A0, A1, A2, A3
  1773. >::type args;
  1774. // Extract first logical parameter.
  1775. typedef typename parameter::value_type<
  1776. args, tag::class_type
  1777. >::type class_type;
  1778. typedef typename parameter::value_type<
  1779. args, tag::base_list, bases<>
  1780. >::type base_list;
  1781. typedef typename parameter::value_type<
  1782. args, tag::held_type, class_type
  1783. >::type held_type;
  1784. typedef typename parameter::value_type<
  1785. args, tag::copyable, void
  1786. >::type copyable;
  1787. };
  1788. }}
  1789. ''') -->
  1790. <p>It may seem like we've added a great deal of complexity, but the benefits to
  1791. our users are greater. Our original examples can now be written without
  1792. explicit parameter names:</p>
  1793. <pre class="literal-block">
  1794. typedef boost::python::class_&lt;<strong>B</strong>, <strong>boost::noncopyable</strong>&gt; c1;
  1795. typedef boost::python::class_&lt;
  1796. <strong>D</strong>, <strong>std::auto_ptr&lt;D&gt;</strong>, <strong>bases&lt;B&gt;</strong>
  1797. &gt; c2;
  1798. </pre>
  1799. <!-- @example.prepend('''
  1800. struct B
  1801. {
  1802. };
  1803. struct D
  1804. {
  1805. };
  1806. using boost::python::bases;
  1807. ''') -->
  1808. <!-- @example.append('''
  1809. BOOST_MPL_ASSERT((boost::is_same<c1::class_type, B>));
  1810. BOOST_MPL_ASSERT((boost::is_same<c1::base_list, bases<> >));
  1811. BOOST_MPL_ASSERT((boost::is_same<c1::held_type, B>));
  1812. BOOST_MPL_ASSERT((
  1813. boost::is_same<c1::copyable, boost::noncopyable>
  1814. ));
  1815. BOOST_MPL_ASSERT((boost::is_same<c2::class_type, D>));
  1816. BOOST_MPL_ASSERT((boost::is_same<c2::base_list, bases<B> >));
  1817. BOOST_MPL_ASSERT((
  1818. boost::is_same<c2::held_type, std::auto_ptr<D> >
  1819. ));
  1820. BOOST_MPL_ASSERT((boost::is_same<c2::copyable, void>));
  1821. ''') -->
  1822. <!-- @test('compile', howmany='all') -->
  1823. </div>
  1824. </div>
  1825. </div>
  1826. <div class="section" id="advanced-topics">
  1827. <h1><a class="toc-backref" href="#id32">3&nbsp;&nbsp;&nbsp;Advanced Topics</a></h1>
  1828. <p>At this point, you should have a good grasp of the basics. In this section
  1829. we'll cover some more esoteric uses of the library.</p>
  1830. <div class="section" id="fine-grained-name-control">
  1831. <h2><a class="toc-backref" href="#id33">3.1&nbsp;&nbsp;&nbsp;Fine-Grained Name Control</a></h2>
  1832. <p>If you don't like the leading-underscore naming convention used to refer to
  1833. keyword objects, or you need the name <tt class="docutils literal">tag</tt> for something other than the
  1834. keyword type namespace, there's another way to use <tt class="docutils literal">BOOST_PARAMETER_NAME</tt>:</p>
  1835. <pre class="literal-block">
  1836. BOOST_PARAMETER_NAME(
  1837. <strong>(</strong>
  1838. <em>object-name</em>
  1839. <strong>,</strong> <em>tag-namespace</em>
  1840. <strong>)</strong> <em>parameter-name</em>
  1841. )
  1842. </pre>
  1843. <!-- @ignore() -->
  1844. <p>Here is a usage example:</p>
  1845. <pre class="literal-block">
  1846. BOOST_PARAMETER_NAME(
  1847. (
  1848. <strong>pass_foo</strong>, <strong>keywords</strong>
  1849. ) <strong>foo</strong>
  1850. )
  1851. BOOST_PARAMETER_FUNCTION(
  1852. (int), f,
  1853. <strong>keywords</strong>, (required (<strong>foo</strong>, *))
  1854. )
  1855. {
  1856. return <strong>foo</strong> + 1;
  1857. }
  1858. int x = f(<strong>pass_foo</strong> = 41);
  1859. </pre>
  1860. <!-- @example.prepend('#include <boost/parameter.hpp>') -->
  1861. <!-- @example.append('''
  1862. int main()
  1863. {
  1864. return 0;
  1865. }
  1866. ''') -->
  1867. <!-- @test('run') -->
  1868. <p>Before you use this more verbose form, however, please read the section on
  1869. <a class="reference internal" href="#keyword-naming">best practices for keyword object naming</a>.</p>
  1870. </div>
  1871. <div class="section" id="more-argumentpacks">
  1872. <h2><a class="toc-backref" href="#id34">3.2&nbsp;&nbsp;&nbsp;More <span class="concept">ArgumentPack</span>s</a></h2>
  1873. <p>We've already seen <span class="concept">ArgumentPack</span>s when we looked at
  1874. <a class="reference internal" href="#parameter-enabled-constructors">parameter-enabled constructors</a> and <a class="reference internal" href="#binding-intro">class templates</a>. As you
  1875. might have guessed, <span class="concept">ArgumentPack</span>s actually lie at the heart of
  1876. everything this library does; in this section we'll examine ways to
  1877. build and manipulate them more effectively.</p>
  1878. <div class="section" id="building-argumentpacks">
  1879. <h3>3.2.1&nbsp;&nbsp;&nbsp;Building <span class="concept">ArgumentPack</span>s</h3>
  1880. <p>The simplest <span class="concept">ArgumentPack</span> is the result of assigning into a keyword object:</p>
  1881. <pre class="literal-block">
  1882. BOOST_PARAMETER_NAME(index)
  1883. template &lt;typename ArgumentPack&gt;
  1884. int print_index(ArgumentPack const&amp; args)
  1885. {
  1886. std::cout &lt;&lt; &quot;index = &quot; &lt;&lt; args[_index];
  1887. std::cout &lt;&lt; std::endl;
  1888. return 0;
  1889. }
  1890. int x = print_index(_index = 3); // prints &quot;index = 3&quot;
  1891. </pre>
  1892. <!-- @example.prepend('''
  1893. #include <boost/parameter.hpp>
  1894. #include <iostream>
  1895. ''') -->
  1896. <p>Also, <span class="concept">ArgumentPack</span>s can be composed using the comma operator. The extra
  1897. parentheses below are used to prevent the compiler from seeing two separate
  1898. arguments to <tt class="docutils literal">print_name_and_index</tt>:</p>
  1899. <pre class="literal-block">
  1900. BOOST_PARAMETER_NAME(name)
  1901. template &lt;typename ArgumentPack&gt;
  1902. int print_name_and_index(ArgumentPack const&amp; args)
  1903. {
  1904. std::cout &lt;&lt; &quot;name = &quot; &lt;&lt; args[_name];
  1905. std::cout &lt;&lt; &quot;; &quot;;
  1906. return print_index(args);
  1907. }
  1908. int y = print_name_and_index((_index = 3, _name = &quot;jones&quot;));
  1909. </pre>
  1910. <p>The <a class="reference external" href="../../test/compose.cpp">compose.cpp</a> test program shows more examples of this feature.</p>
  1911. <p>To build an <span class="concept">ArgumentPack</span> with positional arguments, we can use a
  1912. <a class="reference external" href="reference.html#parameterspec"><span class="concept">ParameterSpec</span></a>. As introduced described in the section on <a class="reference internal" href="#class-template-signatures">Class Template
  1913. Signatures</a>, a <span class="concept">ParameterSpec</span> describes the positional order of parameters
  1914. and any associated type requirements. Just as we can build an <span class="concept">ArgumentPack</span>
  1915. <em>type</em> with its nested <tt class="docutils literal">::bind&lt; … &gt;</tt> template, we can build an
  1916. <span class="concept">ArgumentPack</span> <em>object</em> by invoking its function call operator:</p>
  1917. <pre class="literal-block">
  1918. parameter::parameters&lt;
  1919. required&lt;tag::name, is_convertible&lt;_,char const*&gt; &gt;
  1920. , optional&lt;tag::index, is_convertible&lt;_,int&gt; &gt;
  1921. &gt; spec;
  1922. char const sam[] = &quot;sam&quot;;
  1923. int twelve = 12;
  1924. int z0 = print_name_and_index(
  1925. <strong>spec(</strong> sam, twelve <strong>)</strong>
  1926. );
  1927. int z1 = print_name_and_index(
  1928. <strong>spec(</strong> _index=12, _name=&quot;sam&quot; <strong>)</strong>
  1929. );
  1930. </pre>
  1931. <!-- @example.prepend('''
  1932. namespace parameter = boost::parameter;
  1933. using parameter::required;
  1934. using parameter::optional;
  1935. using boost::is_convertible;
  1936. using boost::mpl::_;
  1937. ''') -->
  1938. <!-- @example.append('''
  1939. int main()
  1940. {
  1941. return 0;
  1942. }
  1943. ''') -->
  1944. <!-- @test('run', howmany='all') -->
  1945. </div>
  1946. <div class="section" id="extracting-parameter-types">
  1947. <h3>3.2.2&nbsp;&nbsp;&nbsp;Extracting Parameter Types</h3>
  1948. <p>If we want to know the types of the arguments passed to
  1949. <tt class="docutils literal">print_name_and_index</tt>, we have a couple of options. The
  1950. simplest and least error-prone approach is to forward them to a
  1951. function template and allow <em>it</em> to do type deduction:</p>
  1952. <pre class="literal-block">
  1953. BOOST_PARAMETER_NAME(name)
  1954. BOOST_PARAMETER_NAME(index)
  1955. template &lt;typename Name, typename Index&gt;
  1956. int deduce_arg_types_impl(Name&amp;&amp; name, Index&amp;&amp; index)
  1957. {
  1958. // we know the types
  1959. Name&amp;&amp; n2 = boost::forward&lt;Name&gt;(name);
  1960. Index&amp;&amp; i2 = boost::forward&lt;Index&gt;(index);
  1961. return index;
  1962. }
  1963. template &lt;typename ArgumentPack&gt;
  1964. int deduce_arg_types(ArgumentPack const&amp; args)
  1965. {
  1966. return deduce_arg_types_impl(args[_name], args[_index | 42]);
  1967. }
  1968. </pre>
  1969. <!-- @example.prepend('''
  1970. #include <boost/parameter.hpp>
  1971. ''') -->
  1972. <!-- @example.append('''
  1973. #include <boost/core/lightweight_test.hpp>
  1974. int main()
  1975. {
  1976. int a1 = deduce_arg_types((_name = "foo"));
  1977. int a2 = deduce_arg_types((_name = "foo", _index = 3));
  1978. BOOST_TEST_EQ(a1, 42);
  1979. BOOST_TEST_EQ(a2, 3);
  1980. return boost::report_errors();
  1981. }
  1982. ''') -->
  1983. <!-- @test('run') -->
  1984. <p>Occasionally one needs to deduce argument types without an extra layer of
  1985. function call. For example, suppose we wanted to return twice the value of
  1986. the <tt class="docutils literal">index</tt> parameter? In that case we can use the <tt class="docutils literal">value_type&lt; … &gt;</tt>
  1987. metafunction introduced <a class="reference internal" href="#binding-intro">earlier</a>:</p>
  1988. <pre class="literal-block">
  1989. BOOST_PARAMETER_NAME(index)
  1990. template &lt;typename ArgumentPack&gt;
  1991. typename boost::parameter::value_type&lt;ArgumentPack,tag::index,int&gt;::type
  1992. twice_index(ArgumentPack const&amp; args)
  1993. {
  1994. return 2 * args[_index | 42];
  1995. }
  1996. </pre>
  1997. <!-- @example.prepend('''
  1998. #include <boost/parameter.hpp>
  1999. ''') -->
  2000. <!-- @example.append('''
  2001. #include <boost/core/lightweight_test.hpp>
  2002. int main()
  2003. {
  2004. int six = twice_index(_index = 3);
  2005. BOOST_TEST_EQ(six, 6);
  2006. return boost::report_errors();
  2007. }
  2008. ''') -->
  2009. <!-- @test('run', howmany='all') -->
  2010. <p>Note that if we had used <tt class="docutils literal">binding&lt; … &gt;</tt> rather than <tt class="docutils literal">value_type&lt; … &gt;</tt>, we
  2011. would end up returning a reference to the temporary created in the <tt class="docutils literal">2 * …</tt>
  2012. expression.</p>
  2013. </div>
  2014. <div class="section" id="lazy-default-computation">
  2015. <h3>3.2.3&nbsp;&nbsp;&nbsp;Lazy Default Computation</h3>
  2016. <p>When a default value is expensive to compute, it would be preferable to avoid
  2017. it until we're sure it's absolutely necessary. <tt class="docutils literal">BOOST_PARAMETER_FUNCTION</tt>
  2018. takes care of that problem for us, but when using <span class="concept">ArgumentPack</span>s
  2019. explicitly, we need a tool other than <tt class="docutils literal">operator|</tt>:</p>
  2020. <pre class="literal-block">
  2021. BOOST_PARAMETER_NAME(s1)
  2022. BOOST_PARAMETER_NAME(s2)
  2023. BOOST_PARAMETER_NAME(s3)
  2024. template &lt;typename ArgumentPack&gt;
  2025. std::string f(ArgumentPack const&amp; args)
  2026. {
  2027. std::string const&amp; s1 = args[_s1];
  2028. std::string const&amp; s2 = args[_s2];
  2029. typename parameter::binding&lt;
  2030. ArgumentPack,tag::s3,std::string
  2031. &gt;::type s3 = args[_s3 | (s1 + s2)]; // always constructs s1 + s2
  2032. return s3;
  2033. }
  2034. std::string x = f((
  2035. _s1=&quot;hello,&quot;, _s2=&quot; world&quot;, _s3=&quot;hi world&quot;
  2036. ));
  2037. </pre>
  2038. <!-- @example.prepend('''
  2039. #include <boost/parameter.hpp>
  2040. #include <string>
  2041. namespace parameter = boost::parameter;
  2042. ''') -->
  2043. <!-- @example.append('''
  2044. int main()
  2045. {
  2046. return 0;
  2047. }
  2048. ''') -->
  2049. <!-- @test('run') -->
  2050. <p>In the example above, the string <tt class="docutils literal">&quot;hello, world&quot;</tt> is constructed despite the
  2051. fact that the user passed us a value for <tt class="docutils literal">s3</tt>. To remedy that, we can
  2052. compute the default value <em>lazily</em> (that is, only on demand), by using
  2053. <tt class="docutils literal"><span class="pre">boost::bind()</span></tt> to create a function object.</p>
  2054. <!-- danielw: I'm leaving the text below in the source, because we might -->
  2055. <!-- want to change back to it after 1.34, and if I remove it now we -->
  2056. <!-- might forget about it. -->
  2057. <!-- by combining the logical-or (“``||``”) operator -->
  2058. <!-- with a function object built by the Boost Lambda_ library: [#bind]_ -->
  2059. <pre class="literal-block">
  2060. typename parameter::binding&lt;
  2061. ArgumentPack,tag::s3,std::string
  2062. &gt;::type s3 = args[
  2063. _s3 <strong>|| boost::bind(
  2064. std::plus&lt;std::string&gt;(), boost::ref(s1), boost::ref(s2)
  2065. )</strong>
  2066. ];
  2067. </pre>
  2068. <!-- @example.prepend('''
  2069. #include <boost/bind.hpp>
  2070. #include <boost/ref.hpp>
  2071. #include <boost/parameter.hpp>
  2072. #include <string>
  2073. #include <functional>
  2074. namespace parameter = boost::parameter;
  2075. BOOST_PARAMETER_NAME(s1)
  2076. BOOST_PARAMETER_NAME(s2)
  2077. BOOST_PARAMETER_NAME(s3)
  2078. template <typename ArgumentPack>
  2079. std::string f(ArgumentPack const& args)
  2080. {
  2081. std::string const& s1 = args[_s1];
  2082. std::string const& s2 = args[_s2];
  2083. ''') -->
  2084. <!-- @example.append('''
  2085. return s3;
  2086. }
  2087. std::string x = f((_s1="hello,", _s2=" world", _s3="hi world"));
  2088. int main()
  2089. {
  2090. return 0;
  2091. }
  2092. ''') -->
  2093. <!-- @test('run') -->
  2094. <!-- .. _Lambda: ../../../lambda/index.html -->
  2095. <div class="sidebar">
  2096. <p class="first sidebar-title">Mnemonics</p>
  2097. <p class="last">To remember the difference between <tt class="docutils literal">|</tt> and <tt class="docutils literal">||</tt>, recall that <tt class="docutils literal">||</tt>
  2098. normally uses short-circuit evaluation: its second argument is only
  2099. evaluated if its first argument is <tt class="docutils literal">false</tt>. Similarly, in
  2100. <tt class="docutils literal">color_map[param || f]</tt>, <tt class="docutils literal">f</tt> is only invoked if no <tt class="docutils literal">color_map</tt>
  2101. argument was supplied.</p>
  2102. </div>
  2103. <p>The expression <tt class="docutils literal"><span class="pre">bind(std::plus&lt;std::string&gt;(),</span> ref(s1), ref(s2))</tt> yields a
  2104. <em>function object</em> that, when invoked, adds the two strings together. That
  2105. function will only be invoked if no <tt class="docutils literal">s3</tt> argument is supplied by the caller.</p>
  2106. <!-- The expression ``lambda::var(s1) + lambda::var(s2)`` yields a -->
  2107. <!-- *function object* that, when invoked, adds the two strings -->
  2108. <!-- together. That function will only be invoked if no ``s3`` argument -->
  2109. <!-- is supplied by the caller. -->
  2110. </div>
  2111. </div>
  2112. </div>
  2113. <div class="section" id="best-practices">
  2114. <h1><a class="toc-backref" href="#id35">4&nbsp;&nbsp;&nbsp;Best Practices</a></h1>
  2115. <p>By now you should have a fairly good idea of how to use the Parameter
  2116. library. This section points out a few more-marginal issues that will help
  2117. you use the library more effectively.</p>
  2118. <div class="section" id="keyword-naming">
  2119. <h2><a class="toc-backref" href="#id36">4.1&nbsp;&nbsp;&nbsp;Keyword Naming</a></h2>
  2120. <p><tt class="docutils literal">BOOST_PARAMETER_NAME</tt> prepends a leading underscore to the names of all our
  2121. keyword objects in order to avoid the following usually-silent bug:</p>
  2122. <pre class="literal-block">
  2123. namespace people
  2124. {
  2125. namespace tag
  2126. {
  2127. struct name
  2128. {
  2129. typedef boost::parameter::forward_reference qualifier;
  2130. };
  2131. struct age
  2132. {
  2133. typedef boost::parameter::forward_reference qualifier;
  2134. };
  2135. }
  2136. namespace // unnamed
  2137. {
  2138. boost::parameter::keyword&lt;tag::name&gt;&amp; <strong>name</strong>
  2139. = boost::parameter::keyword&lt;tag::name&gt;::instance;
  2140. boost::parameter::keyword&lt;tag::age&gt;&amp; <strong>age</strong>
  2141. = boost::parameter::keyword&lt;tag::age&gt;::instance;
  2142. }
  2143. BOOST_PARAMETER_FUNCTION(
  2144. (void), g, tag, (optional (name, *, &quot;bob&quot;)(age, *, 42))
  2145. )
  2146. {
  2147. std::cout &lt;&lt; name &lt;&lt; &quot;:&quot; &lt;&lt; age;
  2148. }
  2149. void f(int age)
  2150. {
  2151. <span class="vellipsis"> .
  2152. .
  2153. .
  2154.  </span>
  2155. g(<strong>age</strong> = 3); // whoops!
  2156. }
  2157. }
  2158. </pre>
  2159. <!-- @ignore() -->
  2160. <p>Although in the case above, the user was trying to pass the value <tt class="docutils literal">3</tt> as the
  2161. <tt class="docutils literal">age</tt> parameter to <tt class="docutils literal">g</tt>, what happened instead was that <tt class="docutils literal">f</tt>'s <tt class="docutils literal">age</tt>
  2162. argument got reassigned the value 3, and was then passed as a positional
  2163. argument to <tt class="docutils literal">g</tt>. Since <tt class="docutils literal">g</tt>'s first positional parameter is <tt class="docutils literal">name</tt>, the
  2164. default value for <tt class="docutils literal">age</tt> is used, and g prints <tt class="docutils literal">3:42</tt>. Our leading
  2165. underscore naming convention makes this problem less likely to occur.</p>
  2166. <p>In this particular case, the problem could have been detected if f's <tt class="docutils literal">age</tt>
  2167. parameter had been made <tt class="docutils literal">const</tt>, which is always a good idea whenever
  2168. possible. Finally, we recommend that you use an enclosing namespace for all
  2169. your code, but particularly for names with leading underscores. If we were to
  2170. leave out the <tt class="docutils literal">people</tt> namespace above, names in the global namespace
  2171. beginning with leading underscores—which are reserved to your C++
  2172. compiler—might become irretrievably ambiguous with those in our
  2173. unnamed namespace.</p>
  2174. </div>
  2175. <div class="section" id="namespaces">
  2176. <h2><a class="toc-backref" href="#id37">4.2&nbsp;&nbsp;&nbsp;Namespaces</a></h2>
  2177. <p>In our examples we've always declared keyword objects in (an unnamed namespace
  2178. within) the same namespace as the Boost.Parameter-enabled functions using
  2179. those keywords:</p>
  2180. <pre class="literal-block">
  2181. namespace lib {
  2182. <strong>BOOST_PARAMETER_NAME(name)
  2183. BOOST_PARAMETER_NAME(index)</strong>
  2184. BOOST_PARAMETER_FUNCTION(
  2185. (int), f, tag,
  2186. (optional (name,*,&quot;bob&quot;)(index,(int),1))
  2187. )
  2188. {
  2189. std::cout &lt;&lt; name &lt;&lt; &quot;:&quot; &lt;&lt; index;
  2190. std::cout &lt;&lt; std::endl;
  2191. return index;
  2192. }
  2193. }
  2194. </pre>
  2195. <!-- @example.prepend('''
  2196. #include <boost/parameter.hpp>
  2197. #include <iostream>
  2198. ''') -->
  2199. <!-- @namespace_setup = str(example) -->
  2200. <!-- @ignore() -->
  2201. <p>Users of these functions have a few choices:</p>
  2202. <ol class="arabic">
  2203. <li><p class="first">Full qualification:</p>
  2204. <pre class="literal-block">
  2205. int x = <strong>lib::</strong>f(
  2206. <strong>lib::</strong>_name = &quot;jill&quot;
  2207. , <strong>lib::</strong>_index = 1
  2208. );
  2209. </pre>
  2210. <p>This approach is more verbose than many users would like.</p>
  2211. </li>
  2212. </ol>
  2213. <!-- @example.prepend(namespace_setup) -->
  2214. <!-- @example.append('int main() { return 0; }') -->
  2215. <!-- @test('run') -->
  2216. <ol class="arabic" start="2">
  2217. <li><p class="first">Make keyword objects available through <em>using-declarations</em>:</p>
  2218. <pre class="literal-block">
  2219. <strong>using lib::_name;
  2220. using lib::_index;</strong>
  2221. int x = lib::f(_name = &quot;jill&quot;, _index = 1);
  2222. </pre>
  2223. <p>This version is much better at the actual call site, but the
  2224. <em>using-declarations</em> themselves can be verbose and hard to manage.</p>
  2225. </li>
  2226. </ol>
  2227. <!-- @example.prepend(namespace_setup) -->
  2228. <!-- @example.append('int main() { return 0; }') -->
  2229. <!-- @test('run') -->
  2230. <ol class="arabic" start="3">
  2231. <li><p class="first">Bring in the entire namespace with a <em>using-directive</em>:</p>
  2232. <pre class="literal-block">
  2233. <strong>using namespace lib;</strong>
  2234. int x = <strong>f</strong>(_name = &quot;jill&quot;, _index = 3);
  2235. </pre>
  2236. <p>This option is convenient, but it indiscriminately makes the <em>entire</em>
  2237. contents of <tt class="docutils literal">lib</tt> available without qualification.</p>
  2238. </li>
  2239. </ol>
  2240. <!-- @example.prepend(namespace_setup) -->
  2241. <!-- @example.append('int main() { return 0; }') -->
  2242. <!-- @test('run') -->
  2243. <p>If we add an additional namespace around keyword declarations, though, we can
  2244. give users more control:</p>
  2245. <pre class="literal-block">
  2246. namespace lib {
  2247. <strong>namespace keywords {</strong>
  2248. BOOST_PARAMETER_NAME(name)
  2249. BOOST_PARAMETER_NAME(index)
  2250. <strong>}</strong>
  2251. BOOST_PARAMETER_FUNCTION(
  2252. (int), f, <strong>keywords::</strong>tag,
  2253. (optional (name,*,&quot;bob&quot;)(index,(int),1))
  2254. )
  2255. {
  2256. std::cout &lt;&lt; name &lt;&lt; &quot;:&quot; &lt;&lt; index;
  2257. std::cout &lt;&lt; std::endl;
  2258. return index;
  2259. }
  2260. }
  2261. </pre>
  2262. <!-- @example.prepend('''
  2263. #include <boost/parameter.hpp>
  2264. #include <iostream>
  2265. ''') -->
  2266. <p>Now users need only a single <em>using-directive</em> to bring in just the names of
  2267. all keywords associated with <tt class="docutils literal">lib</tt>:</p>
  2268. <pre class="literal-block">
  2269. <strong>using namespace lib::keywords;</strong>
  2270. int y = lib::f(_name = &quot;bob&quot;, _index = 2);
  2271. </pre>
  2272. <!-- @example.append('int main() { return 0; }') -->
  2273. <!-- @test('run', howmany='all') -->
  2274. </div>
  2275. <div class="section" id="documentation">
  2276. <h2><a class="toc-backref" href="#id38">4.3&nbsp;&nbsp;&nbsp;Documentation</a></h2>
  2277. <p>The interface idioms enabled by Boost.Parameter are completely new (to C++),
  2278. and as such are not served by pre-existing documentation conventions.</p>
  2279. <div class="note">
  2280. <p class="first admonition-title">Note</p>
  2281. <p class="last">This space is empty because we haven't settled on any best practices
  2282. yet. We'd be very pleased to link to your documentation if you've got a
  2283. style that you think is worth sharing.</p>
  2284. </div>
  2285. </div>
  2286. </div>
  2287. <div class="section" id="portability-considerations">
  2288. <h1><a class="toc-backref" href="#id39">5&nbsp;&nbsp;&nbsp;Portability Considerations</a></h1>
  2289. <p>Use the <a class="reference external" href="http://www.boost.org/regression/release/user/parameter.html">regression test results</a> for the latest Boost release of
  2290. the Parameter library to see how it fares on your favorite
  2291. compiler. Additionally, you may need to be aware of the following
  2292. issues and workarounds for particular compilers.</p>
  2293. <div class="section" id="perfect-forwarding-support">
  2294. <h2><a class="toc-backref" href="#id40">5.1&nbsp;&nbsp;&nbsp;Perfect Forwarding Support</a></h2>
  2295. <p>If your compiler supports <a class="reference external" href="http://www.justsoftwaresolutions.co.uk/cplusplus/rvalue_references_and_perfect_forwarding.html">perfect forwarding</a>, then the Parameter library
  2296. will <tt class="docutils literal">#define</tt> the macro <tt class="docutils literal">BOOST_PARAMETER_HAS_PERFECT_FORWARDING</tt> unless
  2297. you disable it manually. If your compiler does not provide this support, then
  2298. <tt class="docutils literal"><span class="pre">parameter::parameters::operator()</span></tt> will treat rvalue references as lvalue
  2299. <tt class="docutils literal">const</tt> references to work around the <a class="reference external" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1385.htm">forwarding problem</a>, so in certain
  2300. cases you must wrap <a class="reference external" href="../../../core/doc/html/core/ref.html"><tt class="docutils literal"><span class="pre">boost::ref</span></tt></a> or <a class="reference external" href="http://en.cppreference.com/w/cpp/utility/functional/ref"><tt class="docutils literal"><span class="pre">std::ref</span></tt></a> around any arguments that will
  2301. be bound to out parameters. The <a class="reference external" href="../../test/evaluate_category.cpp">evaluate_category.cpp</a> and
  2302. <a class="reference external" href="../../test/preprocessor_eval_category.cpp">preprocessor_eval_category.cpp</a> test programs demonstrate this support.</p>
  2303. </div>
  2304. <div class="section" id="boost-mp11-support">
  2305. <h2><a class="toc-backref" href="#id41">5.2&nbsp;&nbsp;&nbsp;Boost.MP11 Support</a></h2>
  2306. <p>If your compiler is sufficiently compliant with the C++11 standard, then the
  2307. Parameter library will <tt class="docutils literal">#define</tt> the macro <tt class="docutils literal">BOOST_PARAMETER_CAN_USE_MP11</tt>
  2308. unless you disable it manually. The <a class="reference external" href="../../test/singular.cpp">singular.cpp</a>, <a class="reference external" href="../../test/compose.cpp">compose.cpp</a>,
  2309. <a class="reference external" href="../../test/optional_deduced_sfinae.cpp">optional_deduced_sfinae.cpp</a>, and <a class="reference external" href="../../test/deduced_dependent_predicate.cpp">deduced_dependent_predicate.cpp</a> test programs
  2310. demonstrate support for <a class="reference external" href="../../../mp11/doc/html/mp11.html">Boost.MP11</a>.</p>
  2311. </div>
  2312. <div class="section" id="no-sfinae-support">
  2313. <h2><a class="toc-backref" href="#id42">5.3&nbsp;&nbsp;&nbsp;No SFINAE Support</a></h2>
  2314. <p>Some older compilers don't support SFINAE. If your compiler meets that
  2315. criterion, then Boost headers will <tt class="docutils literal">#define</tt> the preprocessor symbol
  2316. <tt class="docutils literal">BOOST_NO_SFINAE</tt>, and parameter-enabled functions won't be removed
  2317. from the overload set based on their signatures. The <a class="reference external" href="../../test/sfinae.cpp">sfinae.cpp</a> and
  2318. <a class="reference external" href="../../test/optional_deduced_sfinae.cpp">optional_deduced_sfinae.cpp</a> test programs demonstrate SFINAE support.</p>
  2319. </div>
  2320. <div class="section" id="no-support-for-result-of">
  2321. <h2>5.4&nbsp;&nbsp;&nbsp;No Support for <a class="reference external" href="../../../utility/utility.htm#result_of"><tt class="docutils literal">result_of</tt></a></h2>
  2322. <p><a class="reference internal" href="#lazy-default-computation">Lazy default computation</a> relies on the <tt class="docutils literal">result_of</tt> class template to
  2323. compute the types of default arguments given the type of the function object
  2324. that constructs them. On compilers that don't support <tt class="docutils literal">result_of</tt>,
  2325. <tt class="docutils literal">BOOST_NO_RESULT_OF</tt> will be <tt class="docutils literal">#define</tt>d, and the compiler will expect
  2326. the function object to contain a nested type name, <tt class="docutils literal">result_type</tt>, that
  2327. indicates its return type when invoked without arguments. To use an ordinary
  2328. function as a default generator on those compilers, you'll need to wrap it in
  2329. a class that provides <tt class="docutils literal">result_type</tt> as a <tt class="docutils literal">typedef</tt> and invokes the
  2330. function via its <tt class="docutils literal">operator()</tt>.</p>
  2331. <!-- Can't Declare |ParameterSpec| via ``typedef``
  2332. =============================================
  2333. In principle you can declare a |ParameterSpec| as a ``typedef`` for a
  2334. specialization of ``parameters<…>``, but Microsoft Visual C++ 6.x has been
  2335. seen to choke on that usage. The workaround is to use inheritance and
  2336. declare your |ParameterSpec| as a class:
  2337. .. parsed-literal::
  2338. **struct dfs_parameters
  2339. :** parameter::parameters<
  2340. tag::graph, tag::visitor, tag::root_vertex
  2341. , tag::index_map, tag::color_map
  2342. >
  2343. **{
  2344. };**
  2345. Default Arguments Unsupported on Nested Templates
  2346. =============================================
  2347. As of this writing, Borland compilers don't support the use of default
  2348. template arguments on member class templates. As a result, you have to
  2349. supply ``BOOST_PARAMETER_MAX_ARITY`` arguments to every use of
  2350. ``parameters<…>::match``. Since the actual defaults used are unspecified,
  2351. the workaround is to use |BOOST_PARAMETER_MATCH|_ to declare default
  2352. arguments for SFINAE.
  2353. .. |BOOST_PARAMETER_MATCH| replace:: ``BOOST_PARAMETER_MATCH`` -->
  2354. </div>
  2355. <div class="section" id="compiler-can-t-see-references-in-unnamed-namespace">
  2356. <h2><a class="toc-backref" href="#id44">5.5&nbsp;&nbsp;&nbsp;Compiler Can't See References In Unnamed Namespace</a></h2>
  2357. <p>If you use Microsoft Visual C++ 6.x, you may find that the compiler has
  2358. trouble finding your keyword objects. This problem has been observed, but
  2359. only on this one compiler, and it disappeared as the test code evolved, so
  2360. we suggest you use it only as a last resort rather than as a preventative
  2361. measure. The solution is to add <em>using-declarations</em> to force the names
  2362. to be available in the enclosing namespace without qualification:</p>
  2363. <pre class="literal-block">
  2364. namespace graphs {
  2365. using graphs::graph;
  2366. using graphs::visitor;
  2367. using graphs::root_vertex;
  2368. using graphs::index_map;
  2369. using graphs::color_map;
  2370. }
  2371. </pre>
  2372. </div>
  2373. </div>
  2374. <div class="section" id="python-binding">
  2375. <h1><a class="toc-backref" href="#id45">6&nbsp;&nbsp;&nbsp;Python Binding</a></h1>
  2376. <p>Follow <a class="reference external" href="../../../parameter_python/doc/html/index.html">this link</a> for documentation on how to expose
  2377. Boost.Parameter-enabled functions to Python with <a class="reference external" href="../../../python/doc/index.html">Boost.Python</a>.</p>
  2378. </div>
  2379. <div class="section" id="reference">
  2380. <h1><a class="toc-backref" href="#id46">7&nbsp;&nbsp;&nbsp;Reference</a></h1>
  2381. <p>Follow <a class="reference external" href="reference.html">this link</a> to the Boost.Parameter reference documentation.</p>
  2382. </div>
  2383. <div class="section" id="glossary">
  2384. <h1><a class="toc-backref" href="#id47">8&nbsp;&nbsp;&nbsp;Glossary</a></h1>
  2385. <div class="section" id="argument-or-actual-argument">
  2386. <span id="arguments"></span><h2><a class="toc-backref" href="#id48">8.1&nbsp;&nbsp;&nbsp;Argument (or “actual argument”)</a></h2>
  2387. <p>the value actually passed to a function or class template.</p>
  2388. </div>
  2389. <div class="section" id="parameter-or-formal-parameter">
  2390. <span id="parameter"></span><h2><a class="toc-backref" href="#id49">8.2&nbsp;&nbsp;&nbsp;Parameter (or “formal parameter”)</a></h2>
  2391. <p>the name used to refer to an argument within a function or class
  2392. template. For example, the value of <tt class="docutils literal">f</tt>'s <em>parameter</em> <tt class="docutils literal">x</tt> is given by the
  2393. <em>argument</em> <tt class="docutils literal">3</tt>:</p>
  2394. <pre class="literal-block">
  2395. int f(int x) { return x + 1; }
  2396. int y = f(3);
  2397. </pre>
  2398. </div>
  2399. </div>
  2400. <div class="section" id="acknowledgements">
  2401. <h1><a class="toc-backref" href="#id50">9&nbsp;&nbsp;&nbsp;Acknowledgements</a></h1>
  2402. <p>The authors would like to thank all the Boosters who participated in the
  2403. review of this library and its documentation, most especially our review
  2404. manager, Doug Gregor.</p>
  2405. <hr class="docutils" />
  2406. <table class="docutils footnote" frame="void" id="old-interface" rules="none">
  2407. <colgroup><col class="label" /><col /></colgroup>
  2408. <tbody valign="top">
  2409. <tr><td class="label"><a class="fn-backref" href="#id3">[1]</a></td><td>As of Boost 1.33.0 the Graph library was still using an
  2410. <a class="reference external" href="../../../graph/doc/bgl_named_params.html">older named parameter mechanism</a>, but there are plans to change it to
  2411. use Boost.Parameter (this library) in an upcoming release, while keeping
  2412. the old interface available for backward-compatibility.</td></tr>
  2413. </tbody>
  2414. </table>
  2415. <table class="docutils footnote" frame="void" id="odr" rules="none">
  2416. <colgroup><col class="label" /><col /></colgroup>
  2417. <tbody valign="top">
  2418. <tr><td class="label"><a class="fn-backref" href="#id5">[2]</a></td><td>The <strong>One Definition Rule</strong> says that any given entity in a C++
  2419. program must have the same definition in all translation units (object
  2420. files) that make up a program.</td></tr>
  2421. </tbody>
  2422. </table>
  2423. <table class="docutils footnote" frame="void" id="vertex-descriptor" rules="none">
  2424. <colgroup><col class="label" /><col /></colgroup>
  2425. <tbody valign="top">
  2426. <tr><td class="label">[3]</td><td>If you're not familiar with the Boost Graph Library,
  2427. don't worry about the meaning of any Graph-library-specific details you
  2428. encounter. In this case you could replace all mentions of vertex
  2429. descriptor types with <tt class="docutils literal">int</tt> in the text, and your understanding of the
  2430. Parameter library wouldn't suffer.</td></tr>
  2431. </tbody>
  2432. </table>
  2433. <table class="docutils footnote" frame="void" id="conceptsts" rules="none">
  2434. <colgroup><col class="label" /><col /></colgroup>
  2435. <tbody valign="top">
  2436. <tr><td class="label"><a class="fn-backref" href="#id7">[4]</a></td><td>This is a major motivation behind <a class="reference external" href="http://en.cppreference.com/w/cpp/language/constraints">C++20 constraints</a>.</td></tr>
  2437. </tbody>
  2438. </table>
  2439. <!-- .. [#bind] The Lambda library is known not to work on `some -->
  2440. <!-- less-conformant compilers`__. When using one of those you could -->
  2441. <!-- use `Boost.Bind`_ to generate the function object\:\: -->
  2442. <!-- boost\:\:bind(std\:\:plus<std\:\:string>(),s1,s2) -->
  2443. <table class="docutils footnote" frame="void" id="is-keyword-expression" rules="none">
  2444. <colgroup><col class="label" /><col /></colgroup>
  2445. <tbody valign="top">
  2446. <tr><td class="label">[5]</td><td><em>(<a class="fn-backref" href="#id13">1</a>, <a class="fn-backref" href="#id14">2</a>)</em> Here we're assuming there's a predicate
  2447. metafunction <tt class="docutils literal">is_keyword_expression</tt> that can be used to identify
  2448. models of Boost.Python's KeywordExpression concept.</td></tr>
  2449. </tbody>
  2450. </table>
  2451. <!-- .. __ http://www.boost.org/regression/release/user/lambda.html -->
  2452. <table class="docutils footnote" frame="void" id="using" rules="none">
  2453. <colgroup><col class="label" /><col /></colgroup>
  2454. <tbody valign="top">
  2455. <tr><td class="label"><a class="fn-backref" href="#id8">[6]</a></td><td><p class="first">You can always give the illusion that the function
  2456. lives in an outer namespace by applying a <em>using-declaration</em>:</p>
  2457. <pre class="literal-block">
  2458. namespace foo_overloads {
  2459. // foo declarations here
  2460. void foo() { ... }
  2461. ...
  2462. }
  2463. using foo_overloads::foo;
  2464. </pre>
  2465. <p class="last">This technique for avoiding unintentional argument-dependent lookup is due
  2466. to Herb Sutter.</p>
  2467. </td></tr>
  2468. </tbody>
  2469. </table>
  2470. <table class="docutils footnote" frame="void" id="sfinae" rules="none">
  2471. <colgroup><col class="label" /><col /></colgroup>
  2472. <tbody valign="top">
  2473. <tr><td class="label">[7]</td><td>This capability depends on your compiler's support for
  2474. SFINAE. <strong>SFINAE</strong>: <strong>S</strong>ubstitution <strong>F</strong>ailure <strong>I</strong>s <strong>N</strong>ot
  2475. <strong>A</strong>n <strong>E</strong>rror. If type substitution during the instantiation of a
  2476. function template results in an invalid type, no compilation error is
  2477. emitted; instead the overload is removed from the overload set. By
  2478. producing an invalid type in the function signature depending on the
  2479. result of some condition, we can decide whether or not an overload is
  2480. considered during overload resolution. The technique is formalized in the
  2481. <a class="reference external" href="../../../core/doc/html/core/enable_if.html"><tt class="docutils literal">enable_if</tt></a> utility. Most recent compilers support SFINAE; on compilers
  2482. that don't support it, the Boost config library will <tt class="docutils literal">#define</tt> the
  2483. symbol <tt class="docutils literal">BOOST_NO_SFINAE</tt>. See
  2484. <a class="reference external" href="http://www.semantics.org/once_weakly/w02_SFINAE.pdf">http://www.semantics.org/once_weakly/w02_SFINAE.pdf</a> for more information
  2485. on SFINAE.</td></tr>
  2486. </tbody>
  2487. </table>
  2488. </div>
  2489. </div>
  2490. <div class="footer">
  2491. <hr class="footer" />
  2492. Generated on: 2019-12-10 00:22 UTC.
  2493. Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
  2494. </div>
  2495. </body>
  2496. </html>