index.rst 88 KB

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