forward.qbk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. [library Boost.Functional/Forward
  2. [quickbook 1.3]
  3. [version 1.0]
  4. [authors [Schwinger, Tobias]]
  5. [copyright 2007 2008 Tobias Schwinger]
  6. [license
  7. Distributed under the Boost Software License, Version 1.0.
  8. (See accompanying file LICENSE_1_0.txt or copy at
  9. [@http://www.boost.org/LICENSE_1_0.txt])
  10. ]
  11. [purpose Function object adapters for generic argument forwarding.]
  12. [category higher-order]
  13. [category generic]
  14. [last-revision $Date: 2008/11/01 19:58:50 $]
  15. ]
  16. [def __unspecified__ /unspecified/]
  17. [def __boost_ref__ [@http://www.boost.org/doc/html/ref.html Boost.Ref]]
  18. [def __boost_result_of__ [@http://www.boost.org/libs/utility/utility.htm#result_of Boost.ResultOf]]
  19. [def __boost__result_of__ [@http://www.boost.org/libs/utility/utility.htm#result_of `boost::result_of`]]
  20. [def __the_forwarding_problem__ [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1385.htm The Forwarding Problem]]
  21. [def __boost_fusion__ [@http://www.boost.org/libs/fusion/doc/html/index.html Boost.Fusion]]
  22. [section Brief Description]
  23. `boost::forward_adapter` provides a reusable adapter template for function
  24. objects. It forwards RValues as references to const, while leaving LValues
  25. as-is.
  26. struct g // function object that only accept LValues
  27. {
  28. template< typename T0, typename T1, typename T2 >
  29. void operator()(T0 & t0, T1 & t1, T2 & t2) const;
  30. typedef void result_type;
  31. };
  32. // Adapted version also accepts RValues and forwards
  33. // them as references to const, LValues as-is
  34. typedef boost::forward_adapter<g> f;
  35. Another adapter, `boost::lighweight_forward_adapter` allows forwarding with
  36. some help from the user accepting and unwrapping reference wrappers (see
  37. __boost_ref__) for reference arguments, const qualifying all other arguments.
  38. The target functions must be compatible with __boost_result_of__, and so are
  39. the adapters.
  40. [endsect]
  41. [section Background]
  42. Let's suppose we have some function `f` that we can call like this:
  43. f(123,a_variable);
  44. Now we want to write another, generic function `g` that can be called the
  45. same way and returns some object that calls `f` with the same arguments.
  46. f(123,a_variable) == g(f,123,a_variable).call_f()
  47. [heading Why would we want to do it, anyway?]
  48. Maybe we want to run `f` several times. Or maybe we want to run it within
  49. another thread. Maybe we just want to encapsulate the call expression for now,
  50. and then use it with other code that allows to compose more complex expressions
  51. in order to decompose it with C++ templates and have the compiler generate some
  52. machinery that eventually calls `f` at runtime (in other words; apply a
  53. technique that is commonly referred to as Expression Templates).
  54. [heading Now, how do we do it?]
  55. The bad news is: It's impossible.
  56. That is so because there is a slight difference between a variable and an
  57. expression that evaluates to its value: Given
  58. int y;
  59. int const z = 0;
  60. and
  61. template< typename T > void func1(T & x);
  62. we can call
  63. func1(y); // x is a reference to a non-const object
  64. func1(z); // x is a reference to a const object
  65. where
  66. func1(1); // fails to compile.
  67. This way we can safely have `func1` store its reference argument and the
  68. compiler keeps us from storing a reference to an object with temporary lifetime.
  69. It is important to realize that non-constness and whether an object binds to a
  70. non-const reference parameter are two different properties. The latter is the
  71. distinction between LValues and RValues. The names stem from the left hand side
  72. and the right hand side of assignment expressions, thus LValues are typically
  73. the ones you can assign to, and RValues the temporary results from the right
  74. hand side expression.
  75. y = 1+2; // a is LValue, 1+2 is the expression producing the RValue,
  76. // 1+2 = a; // usually makes no sense.
  77. func1(y); // works, because y is an LValue
  78. // func1(1+2); // fails to compile, because we only got an RValue.
  79. If we add const qualification on the parameter, our function also accepts
  80. RValues:
  81. template< typename T > void func2(T const & x);
  82. // [...] function scope:
  83. func2(1); // x is a reference to a const temporary, object,
  84. func2(y); // x is a reference to a const object, while y is not const, and
  85. func2(z); // x is a reference to a const object, just like z.
  86. In all cases, the argument `x` in `func2` is a const-qualified LValue.
  87. We can use function overloading to identify non-const LValues:
  88. template< typename T > void func3(T const & x); // #1
  89. template< typename T > void func3(T & x); // #2
  90. // [...] function scope:
  91. func3(1); // x is a reference to a const, temporary object in #1,
  92. func3(y); // x is a reference to a non-const object in #2, and
  93. func3(z); // x is a reference to a const object in #1.
  94. Note that all arguments `x` in the overloaded function `func3` are LValues.
  95. In fact, there is no way to transport RValues into a function as-is in C++98.
  96. Also note that we can't distinguish between what used to be a const qualified
  97. LValue and an RValue.
  98. That's as close as we can get to a generic forwarding function `g` as
  99. described above by the means of C++ 98. See __the_forwarding_problem__ for a
  100. very detailed discussion including solutions that require language changes.
  101. Now, for actually implementing it, we need 2^N overloads for N parameters
  102. (each with and without const qualifier) for each number of arguments
  103. (that is 2^(Nmax+1) - 2^Nmin). Right, that means the compile-time complexity
  104. is O(2^N), however the factor is low so it works quite well for a reasonable
  105. number (< 10) of arguments.
  106. [endsect]
  107. [section:reference Reference]
  108. [section forward_adapter]
  109. [heading Description]
  110. Function object adapter template whose instances are callable with LValue and
  111. RValue arguments. RValue arguments are forwarded as reference-to-const typed
  112. LValues.
  113. An arity can be given as second, numeric non-type template argument to restrict
  114. forwarding to a specific arity.
  115. If a third, numeric non-type template argument is present, the second and third
  116. template argument are treated as minimum and maximum arity, respectively.
  117. Specifying an arity can be helpful to improve the readability of diagnostic
  118. messages and compile time performance.
  119. __boost_result_of__ can be used to determine the result types of specific call
  120. expressions.
  121. [heading Header]
  122. #include <boost/functional/forward_adapter.hpp>
  123. [heading Synopsis]
  124. namespace boost
  125. {
  126. template< class Function,
  127. int Arity_Or_MinArity = __unspecified__, int MaxArity = __unspecified__ >
  128. class forward_adapter;
  129. }
  130. [variablelist Notation
  131. [[`F`] [a possibly const qualified function object type or reference type thereof]]
  132. [[`f`] [an object convertible to `F`]]
  133. [[`FA`] [the type `forward_adapter<F>`]]
  134. [[`fa`] [an instance object of `FA`, initialized with `f`]]
  135. [[`a0`...`aN`] [arguments to `fa`]]
  136. ]
  137. The result type of a target function invocation must be
  138. __boost__result_of__<F*(TA0 [const]&...TAN [const]&])>::type
  139. where `TA0`...`TAN` denote the argument types of `a0`...`aN`.
  140. [heading Expression Semantics]
  141. [table
  142. [[Expression] [Semantics]]
  143. [[`FA(f)`] [creates an adapter, initializes the target function with `f`.]]
  144. [[`FA()`] [creates an adapter, attempts to use `F`'s default constructor.]]
  145. [[`fa(a0`...`aN)`] [calls `f` with with arguments `a0`...`aN`.]]
  146. ]
  147. [heading Limits]
  148. The macro BOOST_FUNCTIONAL_FORWARD_ADAPTER_MAX_ARITY can be defined to set the
  149. maximum call arity. It defaults to 6.
  150. [heading Complexity]
  151. Preprocessing time: O(2^N), where N is the arity limit.
  152. Compile time: O(2^N), where N depends on the arity range.
  153. Run time: O(0) if the compiler inlines, O(1) otherwise.
  154. [endsect]
  155. [section lightweight_forward_adapter]
  156. [heading Description]
  157. Function object adapter template whose instances are callable with LValue and
  158. RValue arguments. All arguments are forwarded as reference-to-const typed
  159. LValues, except for reference wrappers which are unwrapped and may yield
  160. non-const LValues.
  161. An arity can be given as second, numeric non-type template argument to restrict
  162. forwarding to a specific arity.
  163. If a third, numeric non-type template argument is present, the second and third
  164. template argument are treated as minimum and maximum arity, respectively.
  165. Specifying an arity can be helpful to improve the readability of diagnostic
  166. messages and compile time performance.
  167. __boost_result_of__ can be used to determine the result types of specific call
  168. expressions.
  169. [heading Header]
  170. #include <boost/functional/lightweight_forward_adapter.hpp>
  171. [heading Synopsis]
  172. namespace boost
  173. {
  174. template< class Function,
  175. int Arity_Or_MinArity = __unspecified__, int MaxArity = __unspecified__ >
  176. struct lightweight_forward_adapter;
  177. }
  178. [variablelist Notation
  179. [[`F`] [a possibly const qualified function object type or reference type thereof]]
  180. [[`f`] [an object convertible to `F`]]
  181. [[`FA`] [the type `lightweight_forward_adapter<F>`]]
  182. [[`fa`] [an instance of `FA`, initialized with `f`]]
  183. [[`a0`...`aN`] [arguments to `fa`]]
  184. ]
  185. The result type of a target function invocation must be
  186. __boost__result_of__<F*(TA0 [const]&...TAN [const]&])>::type
  187. where `TA0`...`TAN` denote the argument types of `a0`...`aN`.
  188. [heading Expression Semantics]
  189. [table
  190. [[Expression] [Semantics]]
  191. [[`FA(f)`] [creates an adapter, initializes the target function with `f`.]]
  192. [[`FA()`] [creates an adapter, attempts to use `F`'s default constructor.]]
  193. [[`fa(a0`...`aN)`] [calls `f` with with const arguments `a0`...`aN`. If `aI` is a
  194. reference wrapper it is unwrapped.]]
  195. ]
  196. [heading Limits]
  197. The macro BOOST_FUNCTIONAL_LIGHTWEIGHT_FORWARD_ADAPTER_MAX_ARITY can be defined
  198. to set the maximum call arity. It defaults to 10.
  199. [heading Complexity]
  200. Preprocessing time: O(N), where N is the arity limit.
  201. Compile time: O(N), where N is the effective arity of a call.
  202. Run time: O(0) if the compiler inlines, O(1) otherwise.
  203. [endsect]
  204. [endsect]
  205. [section Acknowledgements]
  206. As these utilities are factored out of the __boost_fusion__ functional module,
  207. I want to thank Dan Marsden and Joel de Guzman for letting me participate in the
  208. development of that great library in the first place.
  209. Further, I want to credit the authors of the references below, for their
  210. in-depth investigation of the problem and the solution implemented here.
  211. Last but not least I want to thank Vesa Karnoven and Paul Mensonides for the
  212. Boost Preprocessor library. Without it, I would have ended up with an external
  213. code generator for this one.
  214. [endsect]
  215. [section References]
  216. # [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1385.htm The Forwarding Problem],
  217. Peter Dimov, Howard E. Hinnant, David Abrahams, 2002
  218. # [@http://www.boost.org/libs/utility/utility.htm#result_of Boost.ResultOf],
  219. Douglas Gregor, 2004
  220. # [@http://www.boost.org/doc/html/ref.html Boost.Ref],
  221. Jaakko Jarvi, Peter Dimov, Douglas Gregor, David Abrahams, 1999-2002
  222. [endsect]