functional.qbk 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  1. [/==============================================================================
  2. Copyright (C) 2006 Tobias Schwinger
  3. Use, modification and distribution is subject to the Boost Software
  4. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt)
  6. ===============================================================================/]
  7. [section Functional]
  8. Components to call functions and function objects and to make Fusion code
  9. callable through a function object interface.
  10. [heading Header]
  11. #include <boost/fusion/functional.hpp>
  12. [heading Fused and unfused forms]
  13. What is a function call?
  14. f (a,b,c)
  15. It is a name and a tuple written next to each other, left-to-right.
  16. Although the C++ syntax does not allow to replace [^(a,b,c)] with some Fusion
  17. __sequence__, introducing yet another function provides a solution:
  18. invoke(f,my_sequence)
  19. Alternatively it is possible to apply a simple transformation to [^f] in order
  20. to achieve the same effect:
  21. f tuple <=> ``f'`` (tuple)
  22. Now, [^f'] is an unary function that takes the arguments to `f` as a tuple;
  23. [^f'] is the /fused/ form of `f`.
  24. Reading the above equivalence right-to-left to get the inverse transformation,
  25. `f` is the /unfused/ form of [^f'].
  26. [heading Calling functions and function objects]
  27. Having generic C++ code call back arbitrary functions provided by the client
  28. used to be a heavily repetitive task, as different functions can differ in
  29. arity, invocation syntax and other properties that might be part of the type.
  30. Transporting arguments as Fusion sequences and factoring out the invocation
  31. makes Fusion algorithms applicable to function arguments and also reduces
  32. the problem to one invocation syntax and a fixed arity (instead of an arbitrary
  33. number of arbitrary arguments times several syntactic variants times additional
  34. properties).
  35. Transforming an unfused function into its fused counterpart allows n-ary
  36. calls from an algorithm that invokes an unary __poly_func_obj__ with
  37. __sequence__ arguments.
  38. The library provides several function templates to invoke different kinds of
  39. functions and adapters to transform them into fused form, respectively.
  40. Every variant has a corresponding generator function template that returns
  41. an adapter instance for the given argument.
  42. Constructors can be called applying __boost_func_factory__.
  43. [heading Making Fusion code callable through a function object interface]
  44. Transforming a fused function into its unfused counterpart allows to create
  45. function objects to accept arbitrary calls. In other words, an unary function
  46. object can be implemented instead of (maybe heavily overloaded) function
  47. templates or function call operators.
  48. The library provides both a strictly typed and a generic variant for this
  49. transformation. The latter should be used in combination with
  50. __boost_func_forward__ to attack __the_forwarding_problem__.
  51. Both variants have a corresponding generator function template that returns an
  52. adapter instance for the given argument.
  53. [/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ]
  54. [section Concepts]
  55. [section:callable Callable Object]
  56. [heading Description]
  57. A pointer to a function, a pointer to member function, a pointer to member
  58. data, or a class type whose objects can appear immediately to the left of a
  59. function call operator.
  60. [heading Models]
  61. * function pointer types
  62. * member (function or data) pointer types
  63. * all kinds of function objects
  64. [heading Examples]
  65. & a_free_function
  66. & a_class::a_static_member_function
  67. & a_class::a_nonstatic_data_member
  68. & a_class::a_nonstatic_member_function
  69. std::less<int>()
  70. // using namespace boost;
  71. bind(std::less<int>(), _1, 5)
  72. lambda::_1 += lambda::_2;
  73. fusion::__make_fused_function_object__(std::less<int>())
  74. [endsect]
  75. [section:reg_callable Regular Callable Object]
  76. [heading Description]
  77. A non-member-pointer __callable_obj__ type: A pointer to a function or a class
  78. type whose objects can appear immediately to the left of a function call operator.
  79. [heading Refinement of]
  80. * __callable_obj__
  81. [variablelist Notation
  82. [[`F`][A possibly const qualified Deferred Callable Object type]]
  83. [[`f`][An object or reference to an object of type F]]
  84. [[`A1 ...AN`][Argument types]]
  85. [[`a1 ...aN`][Objects or references to objects with types `A1 ...AN`]]
  86. ]
  87. [heading Expression requirements]
  88. [table
  89. [[Expression][Return Type][Runtime Complexity]]
  90. [[`f(a1, ...aN)`][Unspecified][Unspecified]]
  91. ]
  92. [heading Models]
  93. * function pointer types
  94. * all kinds of function objects
  95. [heading Examples]
  96. & a_free_function
  97. & a_class::a_static_member_function
  98. std::less<int>()
  99. // using namespace boost;
  100. bind(std::less<int>(), _1, 5)
  101. lambda::_1 += lambda::_2;
  102. fusion::__make_fused_function_object__(std::less<int>())
  103. [endsect]
  104. [section:def_callable Deferred Callable Object]
  105. [heading Description]
  106. __callable_obj__ types that work with __boost_result_of__ to determine the
  107. result of a call.
  108. [heading Refinement of]
  109. * __callable_obj__
  110. [blurb note Once C++ supports the [^decltype] keyword, all models of
  111. __callable_obj__ will also be models of __def_callable_obj__, because
  112. function objects won't need client-side support for `result_of`.
  113. ]
  114. [variablelist Notation
  115. [[`F`][A possibly const qualified Deferred Callable Object type]]
  116. [[`A1 ...AN`][Argument types]]
  117. [[`a1 ...aN`][Objects or references to objects with types `A1 ...AN`]]
  118. [[`T1 ...TN`][`T`i is `A`i `&` if `a`i is an __lvalue__, same as `A`i, otherwise]]
  119. ]
  120. [heading Expression requirements]
  121. [table
  122. [[Expression][Type]]
  123. [[__boost_result_of_call__`< F(T1 ...TN) >::type`][Result of a call with `A1 ...AN`-typed arguments]]
  124. ]
  125. [heading Models]
  126. * __poly_func_obj__ types
  127. * member (function or data) pointer types
  128. [heading Examples]
  129. & a_free_function
  130. & a_class::a_static_member_function
  131. & a_class::a_nonstatic_data_member
  132. & a_class::a_nonstatic_member_function
  133. std::less<int>()
  134. // using namespace boost;
  135. bind(std::less<int>(), _1, 5)
  136. // Note: Boost.Lambda expressions don't work with __boost_result_of__
  137. fusion::__make_fused_function_object__(std::less<int>())
  138. [endsect]
  139. [section:poly Polymorphic Function Object]
  140. [heading Description]
  141. A non-member-pointer __def_callable_obj__ type.
  142. [heading Refinement of]
  143. * __reg_callable_obj__
  144. * __def_callable_obj__
  145. [variablelist Notation
  146. [[`F`][A possibly const-qualified Polymorphic Function Object type]]
  147. [[`f`][An object or reference to an object of type F]]
  148. [[`A1 ...AN`][Argument types]]
  149. [[`a1 ...aN`][Objects or references to objects with types `A1 ...AN`]]
  150. [[`T1 ...TN`][`T`i is `A`i `&` if `a`i is an __lvalue__, same as `A`i, otherwise]]
  151. ]
  152. [heading Expression requirements]
  153. [table
  154. [[Expression][Return Type][Runtime Complexity]]
  155. [[`f(a1, ...aN)`][`result_of< F(T1, ...TN) >::type`][Unspecified]]
  156. ]
  157. [heading Models]
  158. * function pointers
  159. * function objects of the Standard Library
  160. * all Fusion __functional_adapters__
  161. [heading Examples]
  162. & a_free_function
  163. & a_class::a_static_member_function
  164. std::less<int>()
  165. // using namespace boost;
  166. bind(std::less<int>(), _1, 5)
  167. // Note: Boost.Lambda expressions don't work with __boost_result_of__
  168. fusion::__make_fused_function_object__(std::less<int>())
  169. [endsect]
  170. [endsect]
  171. [/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ]
  172. [section Invocation]
  173. [section Functions]
  174. [section invoke]
  175. [heading Description]
  176. Calls a __def_callable_obj__ with the arguments from a __sequence__.
  177. The first template parameter can be specialized explicitly to avoid copying
  178. and/or to control the const qualification of a function object.
  179. If the target function is a pointer to a class members, the corresponding
  180. object can be specified as a reference, pointer, or smart pointer.
  181. In case of the latter, a freestanding [^get_pointer] function must be
  182. defined (Boost provides this function for [^std::auto_ptr] and
  183. __boost_shared_ptr_call__).
  184. Constructors can be called applying __boost_func_factory__.
  185. [heading Synopsis]
  186. template<
  187. typename Function,
  188. class Sequence
  189. >
  190. typename __result_of_invoke__<Function, Sequence>::type
  191. invoke(Function f, Sequence & s);
  192. template<
  193. typename Function,
  194. class Sequence
  195. >
  196. typename __result_of_invoke__<Function, Sequence const>::type
  197. invoke(Function f, Sequence const & s);
  198. [heading Parameters]
  199. [table
  200. [[Parameter] [Requirement] [Description]]
  201. [[`f`] [A __def_callable_obj__] [The function to call.]]
  202. [[`s`] [A __forward_sequence__] [The arguments.]]
  203. ]
  204. [heading Expression Semantics]
  205. invoke(f,s);
  206. [*Return type]: Return type of `f` when invoked with the elements in `s` as its
  207. arguments.
  208. [*Semantics]: Invokes `f` with the elements in `s` as arguments and returns
  209. the result of the call expression.
  210. [heading Header]
  211. #include <boost/fusion/functional/invocation/invoke.hpp>
  212. [heading Example]
  213. __std_plus_doc__<int> add;
  214. assert(invoke(add,__make_vector__(1,1)) == 2);
  215. [heading See also]
  216. * __invoke_procedure__
  217. * __invoke_function_object__
  218. * __result_of_invoke__
  219. * __fused__
  220. * __make_fused__
  221. [endsect]
  222. [section:invoke_proc invoke_procedure]
  223. [heading Description]
  224. Calls a __callable_obj__ with the arguments from a __sequence__. The result
  225. of the call is ignored.
  226. The first template parameter can be specialized explicitly to avoid copying
  227. and/or to control the const qualification of a function object.
  228. For pointers to class members corresponding object can be specified as
  229. a reference, pointer, or smart pointer. In case of the latter, a freestanding
  230. [^get_pointer] function must be defined (Boost provides this function for
  231. [^std::auto_ptr] and __boost_shared_ptr_call__).
  232. The target function must not be a pointer to a member object (dereferencing
  233. such a pointer without returning anything does not make sense, so it isn't
  234. implemented).
  235. [heading Synopsis]
  236. template<
  237. typename Function,
  238. class Sequence
  239. >
  240. typename __result_of_invoke_procedure__<Function, Sequence>::type
  241. invoke_procedure(Function f, Sequence & s);
  242. template<
  243. typename Function,
  244. class Sequence
  245. >
  246. typename __result_of_invoke_procedure__<Function, Sequence const>::type
  247. invoke_procedure(Function f, Sequence const & s);
  248. [heading Parameters]
  249. [table
  250. [[Parameter] [Requirement] [Description]]
  251. [[`f`] [Model of __callable_obj__] [The function to call.]]
  252. [[`s`] [Model of __forward_sequence__] [The arguments.]]
  253. ]
  254. [heading Expression Semantics]
  255. invoke_procedure(f,s);
  256. [*Return type]: `void`
  257. [*Semantics]: Invokes `f` with the elements in `s` as arguments.
  258. [heading Header]
  259. #include <boost/fusion/functional/invocation/invoke_procedure.hpp>
  260. [heading Example]
  261. __vector__<int,int> v(1,2);
  262. using namespace boost::lambda;
  263. invoke_procedure(_1 += _2, v);
  264. assert(__front__(v) == 3);
  265. [heading See also]
  266. * __invoke__
  267. * __invoke_function_object__
  268. * __result_of_invoke_procedure__
  269. * __fused_procedure__
  270. * __make_fused_procedure__
  271. [endsect]
  272. [section:invoke_fobj invoke_function_object]
  273. [heading Description]
  274. Calls a __poly_func_obj__ with the arguments from a __sequence__.
  275. The first template parameter can be specialized explicitly to avoid copying
  276. and/or to control the const qualification of a function object.
  277. Constructors can be called applying __boost_func_factory__.
  278. [heading Synopsis]
  279. template<
  280. typename Function,
  281. class Sequence
  282. >
  283. typename __result_of_invoke_function_object__<Function, Sequence>::type
  284. invoke_function_object(Function f, Sequence & s);
  285. template<
  286. typename Function,
  287. class Sequence
  288. >
  289. typename __result_of_invoke_function_object__<Function, Sequence const>::type
  290. invoke_function_object(Function f, Sequence const & s);
  291. [heading Parameters]
  292. [table
  293. [[Parameter] [Requirement] [Description]]
  294. [[`f`] [Model of __poly_func_obj__] [The function object to call.]]
  295. [[`s`] [Model of __forward_sequence__] [The arguments.]]
  296. ]
  297. [heading Expression Semantics]
  298. invoke_function_object(f,s);
  299. [*Return type]: Return type of `f` when invoked with the elements in `s` as its
  300. arguments.
  301. [*Semantics]: Invokes `f` with the elements in `s` as arguments and returns the
  302. result of the call expression.
  303. [heading Header]
  304. #include <boost/fusion/functional/invocation/invoke_function_object.hpp>
  305. [heading Example]
  306. struct sub
  307. {
  308. template <typename Sig>
  309. struct result;
  310. template <class Self, typename T>
  311. struct result< Self(T,T) >
  312. { typedef typename remove_reference<T>::type type; };
  313. template<typename T>
  314. T operator()(T lhs, T rhs) const
  315. {
  316. return lhs - rhs;
  317. }
  318. };
  319. void try_it()
  320. {
  321. sub f;
  322. assert(f(2,1) == invoke_function_object(f,__make_vector__(2,1)));
  323. }
  324. [heading See also]
  325. * __invoke__
  326. * __invoke_procedure__
  327. * __result_of_invoke_function_object__
  328. * __fused_function_object__
  329. * __make_fused_function_object__
  330. [endsect]
  331. [endsect] [/ Functions]
  332. [section Metafunctions]
  333. [section invoke]
  334. [heading Description]
  335. Returns the result type of __invoke__.
  336. [heading Synopsis]
  337. namespace result_of
  338. {
  339. template<
  340. typename Function,
  341. class Sequence
  342. >
  343. struct invoke
  344. {
  345. typedef __unspecified__ type;
  346. };
  347. }
  348. [heading See also]
  349. * __invoke__
  350. * __fused__
  351. [endsect]
  352. [section:invoke_proc invoke_procedure]
  353. [heading Description]
  354. Returns the result type of __invoke_procedure__.
  355. [heading Synopsis]
  356. namespace result_of
  357. {
  358. template<
  359. typename Function,
  360. class Sequence
  361. >
  362. struct invoke_procedure
  363. {
  364. typedef __unspecified__ type;
  365. };
  366. }
  367. [heading See also]
  368. * __invoke_procedure__
  369. * __fused_procedure__
  370. [endsect]
  371. [section:invoke_fobj invoke_function_object]
  372. [heading Description]
  373. Returns the result type of __invoke_function_object__.
  374. [heading Synopsis]
  375. namespace result_of
  376. {
  377. template<
  378. class Function,
  379. class Sequence
  380. >
  381. struct invoke_function_object
  382. {
  383. typedef __unspecified__ type;
  384. };
  385. }
  386. [heading See also]
  387. * __invoke_function_object__
  388. * __fused_function_object__
  389. [endsect]
  390. [endsect] [/ Metafunctions ]
  391. [section Limits]
  392. [heading Header]
  393. #include <boost/fusion/functional/invocation/limits.hpp>
  394. [heading Macros]
  395. The following macros can be defined to change the maximum arity.
  396. The default is 6.
  397. * BOOST_FUSION_INVOKE_MAX_ARITY
  398. * BOOST_FUSION_INVOKE_PROCEDURE_MAX_ARITY
  399. * BOOST_FUSION_INVOKE_FUNCTION_OBJECT_MAX_ARITY
  400. [endsect]
  401. [endsect] [/ Invocation ]
  402. [/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ]
  403. [section:adapters Adapters]
  404. Function object templates to transform a particular target function.
  405. [section fused]
  406. [heading Description]
  407. An unary __poly_func_obj__ adapter template for __def_callable_obj__ target
  408. functions. It takes a __forward_sequence__ that contains the arguments for the
  409. target function.
  410. The type of the target function is allowed to be const qualified or a
  411. reference. Const qualification is preserved and propagated appropriately
  412. (in other words, only const versions of [^operator()] can be used for a
  413. target function object that is const or, if the target function object
  414. is held by value, the adapter is const - these semantics have nothing to
  415. do with the const qualification of a member function, which is referring
  416. to the type of object pointed to by [^this] which is specified with the
  417. first element in the sequence passed to the adapter).
  418. If the target function is a pointer to a class members, the corresponding
  419. object can be specified as a reference, pointer, or smart pointer.
  420. In case of the latter, a freestanding [^get_pointer] function must be
  421. defined (Boost provides this function for [^std::auto_ptr] and
  422. __boost_shared_ptr_call__).
  423. [heading Header]
  424. #include <boost/fusion/functional/adapter/fused.hpp>
  425. [heading Synopsis]
  426. template <typename Function>
  427. class fused;
  428. [heading Template parameters]
  429. [table
  430. [[Parameter] [Description] [Default]]
  431. [[`Function`] [A __def_callable_obj__] []]
  432. ]
  433. [heading Model of]
  434. * __poly_func_obj__
  435. * __def_callable_obj__
  436. [variablelist Notation
  437. [[`R`] [A possibly const qualified __def_callable_obj__ type or reference type thereof]]
  438. [[`r`] [An object convertible to `R`]]
  439. [[`s`] [A __sequence__ of arguments that are accepted by `r`]]
  440. [[`f`] [An instance of `fused<R>`]]
  441. ]
  442. [heading Expression Semantics]
  443. [table
  444. [[Expression] [Semantics]]
  445. [[`fused<R>(r)`] [Creates a fused function as described above, initializes the target function with `r`.]]
  446. [[`fused<R>()`] [Creates a fused function as described above, attempts to use `R`'s default constructor.]]
  447. [[`f(s)`] [Calls `r` with the elements in `s` as its arguments.]]
  448. ]
  449. [heading Example]
  450. fused< __std_plus_doc__<long> > f;
  451. assert(f(__make_vector__(1,2l)) == 3l);
  452. [heading See also]
  453. * __fused_procedure__
  454. * __fused_function_object__
  455. * __invoke__
  456. * __make_fused__
  457. * __deduce__
  458. [endsect]
  459. [section fused_procedure]
  460. [heading Description]
  461. An unary __poly_func_obj__ adapter template for __callable_obj__ target
  462. functions. It takes a __forward_sequence__ that contains the arguments for
  463. the target function.
  464. The result is discarded and the adapter's return type is `void`.
  465. The type of the target function is allowed to be const qualified or a
  466. reference. Const qualification is preserved and propagated appropriately
  467. (in other words, only const versions of [^operator()] can be used for a
  468. target function object that is const or, if the target function object
  469. is held by value, the adapter is const - these semantics have nothing to
  470. do with the const qualification of a member function, which is referring
  471. to the type of object pointed to by [^this] which is specified with the
  472. first element in the sequence passed to the adapter).
  473. If the target function is a pointer to a members function, the corresponding
  474. object can be specified as a reference, pointer, or smart pointer.
  475. In case of the latter, a freestanding [^get_pointer] function must be
  476. defined (Boost provides this function for [^std::auto_ptr] and
  477. __boost_shared_ptr_call__).
  478. The target function must not be a pointer to a member object (dereferencing
  479. such a pointer without returning anything does not make sense, so this case
  480. is not implemented).
  481. [heading Header]
  482. #include <boost/fusion/functional/adapter/fused_procedure.hpp>
  483. [heading Synopsis]
  484. template <typename Function>
  485. class fused_procedure;
  486. [heading Template parameters]
  487. [table
  488. [[Parameter] [Description] [Default]]
  489. [[`Function`] [__callable_obj__ type] []]
  490. ]
  491. [heading Model of]
  492. * __poly_func_obj__
  493. * __def_callable_obj__
  494. [variablelist Notation
  495. [[`R`] [A possibly const qualified __callable_obj__ type or reference type thereof]]
  496. [[`r`] [An object convertible to `R`]]
  497. [[`s`] [A __sequence__ of arguments that are accepted by `r`]]
  498. [[`f`] [An instance of `fused_procedure<R>`]]
  499. ]
  500. [heading Expression Semantics]
  501. [table
  502. [[Expression] [Semantics]]
  503. [[`fused_procedure<R>(r)`] [Creates a fused function as described above, initializes the target function with `r`.]]
  504. [[`fused_procedure<R>()`] [Creates a fused function as described above, attempts to use `R`'s default constructor.]]
  505. [[`f(s)`] [Calls `r` with the elements in `s` as its arguments.]]
  506. ]
  507. [heading Example]
  508. template<class SequenceOfSequences, class Func>
  509. void n_ary_for_each(SequenceOfSequences const & s, Func const & f)
  510. {
  511. __for_each__(__zip_view__<SequenceOfSequences>(s),
  512. fused_procedure<Func const &>(f));
  513. }
  514. void try_it()
  515. {
  516. __vector__<int,float> a(2,2.0f);
  517. __vector__<int,float> b(1,1.5f);
  518. using namespace boost::lambda;
  519. n_ary_for_each(__vector_tie__(a,b), _1 -= _2);
  520. assert(a == __make_vector__(1,0.5f));
  521. }
  522. [heading See also]
  523. * __fused__
  524. * __fused_function_object__
  525. * __invoke_procedure__
  526. * __make_fused_procedure__
  527. [endsect]
  528. [section fused_function_object]
  529. [heading Description]
  530. An unary __poly_func_obj__ adapter template for a __poly_func_obj__ target
  531. function. It takes a __forward_sequence__ that contains the arguments for the
  532. target function.
  533. The type of the target function is allowed to be const qualified or a
  534. reference. Const qualification is preserved and propagated appropriately
  535. (in other words, only const versions of [^operator()] can be used for an
  536. target function object that is const or, if the target function object
  537. is held by value, the adapter is const).
  538. [heading Header]
  539. #include <boost/fusion/functional/adapter/fused_function_object.hpp>
  540. [heading Synopsis]
  541. template <class Function>
  542. class fused_function_object;
  543. [heading Template parameters]
  544. [table
  545. [[Parameter] [Description] [Default]]
  546. [[`Function`] [__poly_func_obj__ type] []]
  547. ]
  548. [heading Model of]
  549. * __poly_func_obj__
  550. * __def_callable_obj__
  551. [variablelist Notation
  552. [[`R`] [A possibly const qualified __poly_func_obj__ type or reference type thereof]]
  553. [[`r`] [An object convertible to `R`]]
  554. [[`s`] [A __sequence__ of arguments that are accepted by `r`]]
  555. [[`f`] [An instance of `fused<R>`]]
  556. ]
  557. [heading Expression Semantics]
  558. [table
  559. [[Expression] [Semantics]]
  560. [[`fused_function_object<R>(r)`] [Creates a fused function as described above, initializes the target function with `r`.]]
  561. [[`fused_function_object<R>()`] [Creates a fused function as described above, attempts to use `R`'s default constructor.]]
  562. [[`f(s)`] [Calls `r` with the elements in `s` as its arguments.]]
  563. ]
  564. [heading Example]
  565. template<class SeqOfSeqs, class Func>
  566. typename __result_of_transform__< zip_view<SeqOfSeqs> const,
  567. fused_function_object<Func const &> >::type
  568. n_ary_transform(SeqOfSeqs const & s, Func const & f)
  569. {
  570. return __transform__(zip_view<SeqOfSeqs>(s),
  571. fused_function_object<Func const &>(f));
  572. }
  573. struct sub
  574. {
  575. template <typename Sig>
  576. struct result;
  577. template <class Self, typename T>
  578. struct result< Self(T,T) >
  579. { typedef typename remove_reference<T>::type type; };
  580. template<typename T>
  581. T operator()(T lhs, T rhs) const
  582. {
  583. return lhs - rhs;
  584. }
  585. };
  586. void try_it()
  587. {
  588. __vector__<int,float> a(2,2.0f);
  589. __vector__<int,float> b(1,1.5f);
  590. __vector__<int,float> c(1,0.5f);
  591. assert(c == n_ary_transform(__vector_tie__(a,b), sub()));
  592. }
  593. [heading See also]
  594. * __fused__
  595. * __fused_procedure__
  596. * __invoke_function_object__
  597. * __make_fused_function_object__
  598. * __deduce__
  599. [endsect]
  600. [section unfused]
  601. [heading Description]
  602. An n-ary __poly_func_obj__ adapter template for an unary __poly_func_obj__
  603. target function. When called, its arguments are bundled to a
  604. __random_access_sequence__ of references that is passed to the target function
  605. object.
  606. The nullary overload of the call operator can be removed by setting the
  607. second template parameter to `false`, which is very useful if the result type
  608. computation would result in a compile error, otherwise (nullary call
  609. operator's prototypes can't be templates and thus are instantiated as early
  610. as the class template).
  611. Only __lvalue__ arguments are accepted. To overcome this limitation, apply
  612. __boost_func_forward__.
  613. The type of the target function is allowed to be const qualified or a
  614. reference. Const qualification is preserved and propagated appropriately.
  615. In other words, only const versions of [^operator()] can be used if
  616. the target function object is const - or, in case the target function
  617. object is held by value, the adapter is const.
  618. [heading Header]
  619. #include <boost/fusion/functional/adapter/unfused.hpp>
  620. [heading Synopsis]
  621. template <class Function, bool AllowNullary = true>
  622. class unfused;
  623. [heading Template parameters]
  624. [table
  625. [[Parameter] [Description] [Default]]
  626. [[`Function`] [A unary __poly_func_obj__] []]
  627. [[`AllowNullary`] [Boolean constant] [true]]
  628. ]
  629. [heading Model of]
  630. * __poly_func_obj__
  631. * __def_callable_obj__
  632. [variablelist Notation
  633. [[`F`] [A possibly const qualified, unary __poly_func_obj__ type or reference type thereof]]
  634. [[`f`] [An object convertible to `F`]]
  635. [[`UL`] [The type `unfused<F>`]]
  636. [[`ul`] [An instance of `UL`, initialized with `f`]]
  637. [[`a0`...`aN`] [Arguments to `ul`]]
  638. ]
  639. [heading Expression Semantics]
  640. [table
  641. [[Expression] [Semantics]]
  642. [[`UL(f)`] [Creates a fused function as described above, initializes the target function with `f`.]]
  643. [[`UL()`] [Creates a fused function as described above, attempts to use `F`'s default constructor.]]
  644. [[`ul(a0`...`aN)`] [Calls `f` with a __sequence__ that contains references to the arguments `a0`...`aN`.]]
  645. ]
  646. [heading Example]
  647. struct fused_incrementer
  648. {
  649. template <class Seq>
  650. struct result
  651. {
  652. typedef void type;
  653. };
  654. template <class Seq>
  655. void operator()(Seq const & s) const
  656. {
  657. __for_each__(s,++boost::lambda::_1);
  658. }
  659. };
  660. void try_it()
  661. {
  662. unfused<fused_incrementer> increment;
  663. int a = 2; char b = 'X';
  664. increment(a,b);
  665. assert(a == 3 && b == 'Y');
  666. }
  667. [heading See also]
  668. * __unfused_typed__
  669. * __make_unfused__
  670. [endsect]
  671. [section unfused_typed]
  672. [heading Description]
  673. An n-ary __poly_func_obj__ adapter template for an unary __poly_func_obj__
  674. target function. When called, its arguments are bundled to a
  675. __random_access_sequence__ that is passed to the target function object.
  676. The call operators of resulting function objects are strictly typed
  677. (in other words, non-templatized) with the types from a __sequence__.
  678. The type of the target function is allowed to be const qualified or a
  679. reference. Const qualification is preserved and propagated appropriately
  680. (in other words, only const versions of [^operator()] can be used if
  681. the target function object is const - or, in case the target function object
  682. is held by value, the adapter is const).
  683. [note For Microsoft Visual C++ 7.1 (Visual Studio 2003) the detection
  684. of the Function Object's const qualification easily causes an internal error.
  685. Therefore the adapter is always treated as if it was const. ]
  686. [tip If the type sequence passed to this template contains
  687. non-reference elements, the element is copied only once - the call operator's
  688. signature is optimized automatically to avoid by-value parameters.]
  689. [heading Header]
  690. #include <boost/fusion/functional/adapter/unfused_typed.hpp>
  691. [heading Synopsis]
  692. template <class Function, class Sequence>
  693. class unfused_typed;
  694. [heading Template parameters]
  695. [table
  696. [[Parameter] [Description] [Default]]
  697. [[`Function`] [A unary __poly_func_obj__] []]
  698. [[`Sequence`] [A __sequence__] []]
  699. ]
  700. [heading Model of]
  701. * __poly_func_obj__
  702. * __def_callable_obj__
  703. [variablelist Notation
  704. [[`F`] [A possibly const qualified, unary __poly_func_obj__ type or reference type thereof]]
  705. [[`f`] [An object convertible to `F`]]
  706. [[`S`] [A __sequence__ of parameter types]]
  707. [[`UT`] [The type `unfused_typed<F,S>`]]
  708. [[`ut`] [An instance of `UT`, initialized with `f`]]
  709. [[`a0`...`aN`] [Arguments to `ut`, convertible to the types in `S`]]
  710. ]
  711. [heading Expression Semantics]
  712. [table
  713. [[Expression] [Semantics]]
  714. [[`UT(f)`] [Creates a fused function as described above, initializes the target function with `f`.]]
  715. [[`UT()`] [Creates a fused function as described above, attempts to use `F`'s default constructor.]]
  716. [[`ut(a0`...`aN)`] [Calls `f` with an instance of `S` (or a subsequence of `S` starting at the first element,
  717. if fewer arguments are given and the overload hasn't been disabled) initialized with
  718. `a0`...`aN`.]]
  719. ]
  720. [heading Example]
  721. struct add_assign // applies operator+=
  722. {
  723. typedef void result_type; // for simplicity
  724. template <typename T>
  725. void operator()(T & lhs, T const & rhs) const
  726. {
  727. lhs += rhs;
  728. }
  729. };
  730. template <class Tie>
  731. class fused_parallel_adder
  732. {
  733. Tie tie_dest;
  734. public:
  735. explicit fused_parallel_adder(Tie const & dest)
  736. : tie_dest(dest)
  737. { }
  738. typedef void result_type;
  739. template <class Seq>
  740. void operator()(Seq const & s) const
  741. {
  742. for_each( zip(tie_dest,s), fused<add_assign>() );
  743. }
  744. };
  745. // accepts a tie and creates a typed function object from it
  746. struct fused_parallel_adder_maker
  747. {
  748. template <typename Sig>
  749. struct result;
  750. template <class Self, class Seq>
  751. struct result< Self(Seq) >
  752. {
  753. typedef typename remove_reference<Seq>::type seq;
  754. typedef unfused_typed< fused_parallel_adder<seq>,
  755. typename mpl::transform<seq, remove_reference<_> >::type > type;
  756. };
  757. template <class Seq>
  758. typename result< void(Seq) >::type operator()(Seq const & tie)
  759. {
  760. return typename result< void(Seq) >::type(
  761. fused_parallel_adder<Seq>(tie) );
  762. }
  763. };
  764. unfused<fused_parallel_adder_maker> parallel_add;
  765. void try_it()
  766. {
  767. int a = 2; char b = 'X';
  768. // the second call is strictly typed with the types deduced from the
  769. // first call
  770. parallel_add(a,b)(3,2);
  771. parallel_add(a,b)(3);
  772. parallel_add(a,b)();
  773. assert(a == 8 && b == 'Z');
  774. }
  775. [heading See also]
  776. * __unfused__
  777. * __deduce__
  778. * __deduce_sequence__
  779. [endsect]
  780. [section Limits]
  781. [heading Header]
  782. #include <boost/fusion/functional/adapter/limits.hpp>
  783. [heading Macros]
  784. The following macros can be defined to change the maximum arity.
  785. The value used for these macros must not exceed `FUSION_MAX_VECTOR_SIZE`.
  786. The default is 6.
  787. * BOOST_FUSION_UNFUSED_MAX_ARITY
  788. * BOOST_FUSION_UNFUSED_TYPE_MAX_ARITY
  789. [endsect]
  790. [endsect] [/ Adapters]
  791. [/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ]
  792. [section Generation]
  793. [section Functions]
  794. [section:mk_fused make_fused]
  795. [heading Description]
  796. Creates a __fused__ adapter for a given __def_callable_obj__. The usual
  797. __element_conversion__ is applied to the target function.
  798. [heading Synopsis]
  799. template <typename F>
  800. inline typename __result_of_make_fused__<F>::type
  801. make_fused(F const & f);
  802. [heading Parameters]
  803. [table
  804. [[Parameter] [Requirement] [Description]]
  805. [[`f`] [Model of __def_callable_obj__] [The function to transform.]]
  806. ]
  807. [heading Expression Semantics]
  808. make_fused(f);
  809. [*Return type]: A specialization of __fused__.
  810. [*Semantics]: Returns a __fused__ adapter for `f`.
  811. [heading Header]
  812. #include <boost/fusion/functional/generation/make_fused.hpp>
  813. #include <boost/fusion/include/make_fused.hpp>
  814. [heading Example]
  815. float sub(float a, float b) { return a - b; }
  816. void try_it()
  817. {
  818. __vector__<int,float> a(2,2.0f);
  819. __vector__<int,float> b(1,1.5f);
  820. __vector__<float,float> c(1.0f,0.5f);
  821. assert(c == __transform__(__zip__(a,b), make_fused(& sub)));
  822. assert(c == __transform__(__zip__(a,b), make_fused(__std_minus_doc__<float>())));
  823. }
  824. [heading See also]
  825. * __fused__
  826. * __deduce__
  827. * __result_of_make_fused__
  828. [endsect]
  829. [section:mk_fused_proc make_fused_procedure]
  830. [heading Description]
  831. Creates a __fused_procedure__ adapter for a given __def_callable_obj__.
  832. The usual __element_conversion__ applied to the target function.
  833. [heading Synopsis]
  834. template <typename F>
  835. inline typename __result_of_make_fused_procedure__<F>::type
  836. make_fused_procedure(F const & f);
  837. [heading Parameters]
  838. [table
  839. [[Parameter] [Requirement] [Description]]
  840. [[`f`] [Model of __callable_obj__] [The function to transform.]]
  841. ]
  842. [heading Expression Semantics]
  843. make_fused_procedure(f);
  844. [*Return type]: A specialization of __fused_procedure__.
  845. [*Semantics]: Returns a __fused_procedure__ adapter for `f`.
  846. [heading Header]
  847. #include <boost/fusion/functional/generation/make_fused_procedure.hpp>
  848. #include <boost/fusion/include/make_fused_procedure.hpp>
  849. [heading Example]
  850. __vector__<int,int,int> v(1,2,3);
  851. using namespace boost::lambda;
  852. make_fused_procedure(_1 += _2 - _3)(v);
  853. assert(__front__(v) == 0);
  854. [heading See also]
  855. * __fused_procedure__
  856. * __deduce__
  857. * __result_of_make_fused_procedure__
  858. [endsect]
  859. [section:mk_fused_fobj make_fused_function_object]
  860. [heading Description]
  861. Creates a __fused_function_object__ adapter for a given __def_callable_obj__.
  862. The usual __element_conversion__ is applied to the target function.
  863. [heading Synopsis]
  864. template <typename F>
  865. inline typename __result_of_make_fused_function_object__<F>::type
  866. make_fused_function_object(F const & f);
  867. [heading Parameters]
  868. [table
  869. [[Parameter] [Requirement] [Description]]
  870. [[`f`] [Model of __poly_func_obj__] [The function to transform.]]
  871. ]
  872. [heading Expression Semantics]
  873. make_fused_function_object(f);
  874. [*Return type]: A specialization of __fused_function_object__.
  875. [*Semantics]: Returns a __fused_function_object__ adapter for `f`.
  876. [heading Header]
  877. #include <boost/fusion/functional/generation/make_fused_function_object.hpp>
  878. #include <boost/fusion/include/make_fused_function_object.hpp>
  879. [heading Example]
  880. struct sub
  881. {
  882. template <typename Sig>
  883. struct result;
  884. template <class Self, typename T>
  885. struct result< Self(T,T) >
  886. { typedef typename remove_reference<T>::type type; };
  887. template<typename T>
  888. T operator()(T lhs, T rhs) const
  889. {
  890. return lhs - rhs;
  891. }
  892. };
  893. void try_it()
  894. {
  895. __vector__<int,float> a(2,2.0f);
  896. __vector__<int,float> b(1,1.5f);
  897. __vector__<int,float> c(1,0.5f);
  898. assert(c == __transform__(__zip__(a,b), make_fused_function_object(sub())));
  899. }
  900. [heading See also]
  901. * __fused_function_object__
  902. * __deduce__
  903. * __result_of_make_fused_function_object__
  904. [endsect]
  905. [section:mk_unfused make_unfused]
  906. [heading Description]
  907. Creates a __unfused__ adapter for a given, unary __poly_func_obj__.
  908. The usual __element_conversion__ is applied to the target function.
  909. [heading Synopsis]
  910. template <typename F>
  911. inline typename __result_of_make_unfused__<F>::type
  912. make_unfused(F const & f);
  913. [heading Parameters]
  914. [table
  915. [[Parameter] [Requirement] [Description]]
  916. [[`f`] [Model of __poly_func_obj__] [The function to transform.]]
  917. ]
  918. [heading Expression Semantics]
  919. make_unfused(f);
  920. [*Return type]: A specialization of __unfused__.
  921. [*Semantics]: Returns a __unfused__ adapter for `f`.
  922. [heading Header]
  923. #include <boost/fusion/functional/generation/make_unfused.hpp>
  924. #include <boost/fusion/include/make_unfused.hpp>
  925. [heading Example]
  926. struct fused_incrementer
  927. {
  928. template <class Seq>
  929. struct result
  930. {
  931. typedef void type;
  932. };
  933. template <class Seq>
  934. void operator()(Seq const & s) const
  935. {
  936. __for_each__(s,++boost::lambda::_1);
  937. }
  938. };
  939. void try_it()
  940. {
  941. int a = 2; char b = 'X';
  942. make_unfused(fused_incrementer())(a,b);
  943. assert(a == 3 && b == 'Y');
  944. }
  945. [heading See also]
  946. * __unfused__
  947. * __deduce__
  948. * __result_of_make_unfused__
  949. [endsect]
  950. [endsect] [/ Functions]
  951. [section Metafunctions]
  952. [section:mk_fused make_fused]
  953. [heading Description]
  954. Returns the result type of __make_fused__.
  955. [heading Header]
  956. #include <boost/fusion/functional/generation/make_fused.hpp>
  957. #include <boost/fusion/include/make_fused.hpp>
  958. [heading Synopsis]
  959. namespace result_of
  960. {
  961. template<typename Function>
  962. struct make_fused
  963. {
  964. typedef __unspecified__ type;
  965. };
  966. }
  967. [heading See also]
  968. * __make_fused__
  969. [endsect]
  970. [section:mk_fused_proc make_fused_procedure]
  971. [heading Description]
  972. Returns the result type of __make_fused_procedure__.
  973. [heading Header]
  974. #include <boost/fusion/functional/generation/make_fused_procedure.hpp>
  975. #include <boost/fusion/include/make_fused_procedure.hpp>
  976. [heading Synopsis]
  977. namespace result_of
  978. {
  979. template<typename Function>
  980. struct make_fused_procedure
  981. {
  982. typedef __unspecified__ type;
  983. };
  984. }
  985. [heading See also]
  986. * __make_fused_procedure__
  987. [endsect]
  988. [section:mk_fused_fobj make_fused_function_object]
  989. [heading Description]
  990. Returns the result type of __make_fused_function_object__.
  991. [heading Header]
  992. #include <boost/fusion/functional/generation/make_fused_function_object.hpp>
  993. #include <boost/fusion/include/make_fused_function_object.hpp>
  994. [heading Synopsis]
  995. namespace result_of
  996. {
  997. template<typename Function>
  998. struct make_fused_function_object
  999. {
  1000. typedef __unspecified__ type;
  1001. };
  1002. }
  1003. [heading See also]
  1004. * __make_fused_function_object__
  1005. [endsect]
  1006. [section:mk_unfused make_unfused]
  1007. [heading Description]
  1008. Returns the result type of __make_unfused__.
  1009. [heading Header]
  1010. #include <boost/fusion/functional/generation/make_unfused.hpp>
  1011. #include <boost/fusion/include/make_unfused.hpp>
  1012. [heading Synopsis]
  1013. namespace result_of
  1014. {
  1015. template<typename Function>
  1016. struct make_unfused
  1017. {
  1018. typedef __unspecified__ type;
  1019. };
  1020. }
  1021. [heading See also]
  1022. * __make_unfused__
  1023. [endsect]
  1024. [endsect] [/ Metafunctions]
  1025. [endsect] [/ Generation]
  1026. [endsect] [/ Functional ]