index.html 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Chapter&#160;1.&#160;Boost.Functional/Forward 1.0</title>
  5. <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="index.html" title="Chapter&#160;1.&#160;Boost.Functional/Forward 1.0">
  8. </head>
  9. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  10. <table cellpadding="2" width="100%"><tr>
  11. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
  12. <td align="center"><a href="../../../../../index.html">Home</a></td>
  13. <td align="center"><a href="../../../../libraries.htm">Libraries</a></td>
  14. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  15. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  16. <td align="center"><a href="../../../../../more/index.htm">More</a></td>
  17. </tr></table>
  18. <hr>
  19. <div class="spirit-nav"></div>
  20. <div class="chapter">
  21. <div class="titlepage"><div>
  22. <div><h2 class="title">
  23. <a name="boost_functional_forward"></a>Chapter&#160;1.&#160;Boost.Functional/Forward 1.0</h2></div>
  24. <div><div class="author"><h3 class="author">
  25. <span class="firstname">Tobias</span> <span class="surname">Schwinger</span>
  26. </h3></div></div>
  27. <div><p class="copyright">Copyright &#169; 2007, 2008 Tobias Schwinger</p></div>
  28. <div><div class="legalnotice">
  29. <a name="boost_functional_forward.legal"></a><p>
  30. Distributed under the Boost Software License, Version 1.0. (See accompanying
  31. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  32. </p>
  33. </div></div>
  34. </div></div>
  35. <div class="toc">
  36. <p><b>Table of Contents</b></p>
  37. <dl class="toc">
  38. <dt><span class="section"><a href="index.html#boost_functional_forward.brief_description">Brief Description</a></span></dt>
  39. <dt><span class="section"><a href="index.html#boost_functional_forward.background">Background</a></span></dt>
  40. <dt><span class="section"><a href="index.html#boost_functional_forward.reference">Reference</a></span></dt>
  41. <dt><span class="section"><a href="index.html#boost_functional_forward.acknowledgements">Acknowledgements</a></span></dt>
  42. <dt><span class="section"><a href="index.html#boost_functional_forward.references">References</a></span></dt>
  43. </dl>
  44. </div>
  45. <div class="section">
  46. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  47. <a name="boost_functional_forward.brief_description"></a><a class="link" href="index.html#boost_functional_forward.brief_description" title="Brief Description">Brief Description</a>
  48. </h2></div></div></div>
  49. <p>
  50. <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">forward_adapter</span></code> provides a reusable adapter
  51. template for function objects. It forwards RValues as references to const,
  52. while leaving LValues as-is.
  53. </p>
  54. <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">g</span> <span class="comment">// function object that only accept LValues</span>
  55. <span class="special">{</span>
  56. <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T0</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">T1</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">T2</span> <span class="special">&gt;</span>
  57. <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">T0</span> <span class="special">&amp;</span> <span class="identifier">t0</span><span class="special">,</span> <span class="identifier">T1</span> <span class="special">&amp;</span> <span class="identifier">t1</span><span class="special">,</span> <span class="identifier">T2</span> <span class="special">&amp;</span> <span class="identifier">t2</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
  58. <span class="keyword">typedef</span> <span class="keyword">void</span> <span class="identifier">result_type</span><span class="special">;</span>
  59. <span class="special">};</span>
  60. <span class="comment">// Adapted version also accepts RValues and forwards</span>
  61. <span class="comment">// them as references to const, LValues as-is</span>
  62. <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">forward_adapter</span><span class="special">&lt;</span><span class="identifier">g</span><span class="special">&gt;</span> <span class="identifier">f</span><span class="special">;</span>
  63. </pre>
  64. <p>
  65. Another adapter, <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">lighweight_forward_adapter</span></code> allows forwarding
  66. with some help from the user accepting and unwrapping reference wrappers (see
  67. <a href="http://www.boost.org/doc/html/ref.html" target="_top">Boost.Ref</a>) for
  68. reference arguments, const qualifying all other arguments.
  69. </p>
  70. <p>
  71. The target functions must be compatible with <a href="http://www.boost.org/libs/utility/utility.htm#result_of" target="_top">Boost.ResultOf</a>,
  72. and so are the adapters.
  73. </p>
  74. </div>
  75. <div class="section">
  76. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  77. <a name="boost_functional_forward.background"></a><a class="link" href="index.html#boost_functional_forward.background" title="Background">Background</a>
  78. </h2></div></div></div>
  79. <p>
  80. Let's suppose we have some function <code class="computeroutput"><span class="identifier">f</span></code>
  81. that we can call like this:
  82. </p>
  83. <pre class="programlisting"><span class="identifier">f</span><span class="special">(</span><span class="number">123</span><span class="special">,</span><span class="identifier">a_variable</span><span class="special">);</span>
  84. </pre>
  85. <p>
  86. Now we want to write another, generic function <code class="computeroutput"><span class="identifier">g</span></code>
  87. that can be called the same way and returns some object that calls <code class="computeroutput"><span class="identifier">f</span></code> with the same arguments.
  88. </p>
  89. <pre class="programlisting"><span class="identifier">f</span><span class="special">(</span><span class="number">123</span><span class="special">,</span><span class="identifier">a_variable</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">g</span><span class="special">(</span><span class="identifier">f</span><span class="special">,</span><span class="number">123</span><span class="special">,</span><span class="identifier">a_variable</span><span class="special">).</span><span class="identifier">call_f</span><span class="special">()</span>
  90. </pre>
  91. <h4>
  92. <a name="boost_functional_forward.background.h0"></a>
  93. <span class="phrase"><a name="boost_functional_forward.background.why_would_we_want_to_do_it__anyway_"></a></span><a class="link" href="index.html#boost_functional_forward.background.why_would_we_want_to_do_it__anyway_">Why
  94. would we want to do it, anyway?</a>
  95. </h4>
  96. <p>
  97. Maybe we want to run <code class="computeroutput"><span class="identifier">f</span></code> several
  98. times. Or maybe we want to run it within another thread. Maybe we just want
  99. to encapsulate the call expression for now, and then use it with other code
  100. that allows to compose more complex expressions in order to decompose it with
  101. C++ templates and have the compiler generate some machinery that eventually
  102. calls <code class="computeroutput"><span class="identifier">f</span></code> at runtime (in other
  103. words; apply a technique that is commonly referred to as Expression Templates).
  104. </p>
  105. <h4>
  106. <a name="boost_functional_forward.background.h1"></a>
  107. <span class="phrase"><a name="boost_functional_forward.background.now__how_do_we_do_it_"></a></span><a class="link" href="index.html#boost_functional_forward.background.now__how_do_we_do_it_">Now, how
  108. do we do it?</a>
  109. </h4>
  110. <p>
  111. The bad news is: It's impossible.
  112. </p>
  113. <p>
  114. That is so because there is a slight difference between a variable and an expression
  115. that evaluates to its value: Given
  116. </p>
  117. <pre class="programlisting"><span class="keyword">int</span> <span class="identifier">y</span><span class="special">;</span>
  118. <span class="keyword">int</span> <span class="keyword">const</span> <span class="identifier">z</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
  119. </pre>
  120. <p>
  121. and
  122. </p>
  123. <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">func1</span><span class="special">(</span><span class="identifier">T</span> <span class="special">&amp;</span> <span class="identifier">x</span><span class="special">);</span>
  124. </pre>
  125. <p>
  126. we can call
  127. </p>
  128. <pre class="programlisting"><span class="identifier">func1</span><span class="special">(</span><span class="identifier">y</span><span class="special">);</span> <span class="comment">// x is a reference to a non-const object</span>
  129. <span class="identifier">func1</span><span class="special">(</span><span class="identifier">z</span><span class="special">);</span> <span class="comment">// x is a reference to a const object</span>
  130. </pre>
  131. <p>
  132. where
  133. </p>
  134. <pre class="programlisting"><span class="identifier">func1</span><span class="special">(</span><span class="number">1</span><span class="special">);</span> <span class="comment">// fails to compile.</span>
  135. </pre>
  136. <p>
  137. This way we can safely have <code class="computeroutput"><span class="identifier">func1</span></code>
  138. store its reference argument and the compiler keeps us from storing a reference
  139. to an object with temporary lifetime.
  140. </p>
  141. <p>
  142. It is important to realize that non-constness and whether an object binds to
  143. a non-const reference parameter are two different properties. The latter is
  144. the distinction between LValues and RValues. The names stem from the left hand
  145. side and the right hand side of assignment expressions, thus LValues are typically
  146. the ones you can assign to, and RValues the temporary results from the right
  147. hand side expression.
  148. </p>
  149. <pre class="programlisting"><span class="identifier">y</span> <span class="special">=</span> <span class="number">1</span><span class="special">+</span><span class="number">2</span><span class="special">;</span> <span class="comment">// a is LValue, 1+2 is the expression producing the RValue,</span>
  150. <span class="comment">// 1+2 = a; // usually makes no sense. </span>
  151. <span class="identifier">func1</span><span class="special">(</span><span class="identifier">y</span><span class="special">);</span> <span class="comment">// works, because y is an LValue</span>
  152. <span class="comment">// func1(1+2); // fails to compile, because we only got an RValue.</span>
  153. </pre>
  154. <p>
  155. If we add const qualification on the parameter, our function also accepts RValues:
  156. </p>
  157. <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">func2</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">x</span><span class="special">);</span>
  158. <span class="comment">// [...] function scope:</span>
  159. <span class="identifier">func2</span><span class="special">(</span><span class="number">1</span><span class="special">);</span> <span class="comment">// x is a reference to a const temporary, object,</span>
  160. <span class="identifier">func2</span><span class="special">(</span><span class="identifier">y</span><span class="special">);</span> <span class="comment">// x is a reference to a const object, while y is not const, and</span>
  161. <span class="identifier">func2</span><span class="special">(</span><span class="identifier">z</span><span class="special">);</span> <span class="comment">// x is a reference to a const object, just like z.</span>
  162. </pre>
  163. <p>
  164. In all cases, the argument <code class="computeroutput"><span class="identifier">x</span></code>
  165. in <code class="computeroutput"><span class="identifier">func2</span></code> is a const-qualified
  166. LValue. We can use function overloading to identify non-const LValues:
  167. </p>
  168. <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">func3</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">x</span><span class="special">);</span> <span class="comment">// #1</span>
  169. <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">func3</span><span class="special">(</span><span class="identifier">T</span> <span class="special">&amp;</span> <span class="identifier">x</span><span class="special">);</span> <span class="comment">// #2</span>
  170. <span class="comment">// [...] function scope:</span>
  171. <span class="identifier">func3</span><span class="special">(</span><span class="number">1</span><span class="special">);</span> <span class="comment">// x is a reference to a const, temporary object in #1,</span>
  172. <span class="identifier">func3</span><span class="special">(</span><span class="identifier">y</span><span class="special">);</span> <span class="comment">// x is a reference to a non-const object in #2, and</span>
  173. <span class="identifier">func3</span><span class="special">(</span><span class="identifier">z</span><span class="special">);</span> <span class="comment">// x is a reference to a const object in #1.</span>
  174. </pre>
  175. <p>
  176. Note that all arguments <code class="computeroutput"><span class="identifier">x</span></code> in
  177. the overloaded function <code class="computeroutput"><span class="identifier">func3</span></code>
  178. are LValues. In fact, there is no way to transport RValues into a function
  179. as-is in C++98. Also note that we can't distinguish between what used to be
  180. a const qualified LValue and an RValue.
  181. </p>
  182. <p>
  183. That's as close as we can get to a generic forwarding function <code class="computeroutput"><span class="identifier">g</span></code> as described above by the means of C++
  184. 98. See <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1385.htm" target="_top">The
  185. Forwarding Problem</a> for a very detailed discussion including solutions
  186. that require language changes.
  187. </p>
  188. <p>
  189. Now, for actually implementing it, we need 2^N overloads for N parameters (each
  190. with and without const qualifier) for each number of arguments (that is 2^(Nmax+1)
  191. - 2^Nmin). Right, that means the compile-time complexity is O(2^N), however
  192. the factor is low so it works quite well for a reasonable number (&lt; 10)
  193. of arguments.
  194. </p>
  195. </div>
  196. <div class="section">
  197. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  198. <a name="boost_functional_forward.reference"></a><a class="link" href="index.html#boost_functional_forward.reference" title="Reference">Reference</a>
  199. </h2></div></div></div>
  200. <div class="toc"><dl class="toc">
  201. <dt><span class="section"><a href="index.html#boost_functional_forward.reference.forward_adapter">forward_adapter</a></span></dt>
  202. <dt><span class="section"><a href="index.html#boost_functional_forward.reference.lightweight_forward_adapter">lightweight_forward_adapter</a></span></dt>
  203. </dl></div>
  204. <div class="section">
  205. <div class="titlepage"><div><div><h3 class="title">
  206. <a name="boost_functional_forward.reference.forward_adapter"></a><a class="link" href="index.html#boost_functional_forward.reference.forward_adapter" title="forward_adapter">forward_adapter</a>
  207. </h3></div></div></div>
  208. <h5>
  209. <a name="boost_functional_forward.reference.forward_adapter.h0"></a>
  210. <span class="phrase"><a name="boost_functional_forward.reference.forward_adapter.description"></a></span><a class="link" href="index.html#boost_functional_forward.reference.forward_adapter.description">Description</a>
  211. </h5>
  212. <p>
  213. Function object adapter template whose instances are callable with LValue
  214. and RValue arguments. RValue arguments are forwarded as reference-to-const
  215. typed LValues.
  216. </p>
  217. <p>
  218. An arity can be given as second, numeric non-type template argument to restrict
  219. forwarding to a specific arity. If a third, numeric non-type template argument
  220. is present, the second and third template argument are treated as minimum
  221. and maximum arity, respectively. Specifying an arity can be helpful to improve
  222. the readability of diagnostic messages and compile time performance.
  223. </p>
  224. <p>
  225. <a href="http://www.boost.org/libs/utility/utility.htm#result_of" target="_top">Boost.ResultOf</a>
  226. can be used to determine the result types of specific call expressions.
  227. </p>
  228. <h5>
  229. <a name="boost_functional_forward.reference.forward_adapter.h1"></a>
  230. <span class="phrase"><a name="boost_functional_forward.reference.forward_adapter.header"></a></span><a class="link" href="index.html#boost_functional_forward.reference.forward_adapter.header">Header</a>
  231. </h5>
  232. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">functional</span><span class="special">/</span><span class="identifier">forward_adapter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  233. </pre>
  234. <h5>
  235. <a name="boost_functional_forward.reference.forward_adapter.h2"></a>
  236. <span class="phrase"><a name="boost_functional_forward.reference.forward_adapter.synopsis"></a></span><a class="link" href="index.html#boost_functional_forward.reference.forward_adapter.synopsis">Synopsis</a>
  237. </h5>
  238. <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span>
  239. <span class="special">{</span>
  240. <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">Function</span><span class="special">,</span>
  241. <span class="keyword">int</span> <span class="identifier">Arity_Or_MinArity</span> <span class="special">=</span> <span class="emphasis"><em>unspecified</em></span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">MaxArity</span> <span class="special">=</span> <span class="emphasis"><em>unspecified</em></span> <span class="special">&gt;</span>
  242. <span class="keyword">class</span> <span class="identifier">forward_adapter</span><span class="special">;</span>
  243. <span class="special">}</span>
  244. </pre>
  245. <div class="variablelist">
  246. <p class="title"><b>Notation</b></p>
  247. <dl class="variablelist">
  248. <dt><span class="term"><code class="computeroutput"><span class="identifier">F</span></code></span></dt>
  249. <dd><p>
  250. a possibly const qualified function object type or reference type thereof
  251. </p></dd>
  252. <dt><span class="term"><code class="computeroutput"><span class="identifier">f</span></code></span></dt>
  253. <dd><p>
  254. an object convertible to <code class="computeroutput"><span class="identifier">F</span></code>
  255. </p></dd>
  256. <dt><span class="term"><code class="computeroutput"><span class="identifier">FA</span></code></span></dt>
  257. <dd><p>
  258. the type <code class="computeroutput"><span class="identifier">forward_adapter</span><span class="special">&lt;</span><span class="identifier">F</span><span class="special">&gt;</span></code>
  259. </p></dd>
  260. <dt><span class="term"><code class="computeroutput"><span class="identifier">fa</span></code></span></dt>
  261. <dd><p>
  262. an instance object of <code class="computeroutput"><span class="identifier">FA</span></code>,
  263. initialized with <code class="computeroutput"><span class="identifier">f</span></code>
  264. </p></dd>
  265. <dt><span class="term"><code class="computeroutput"><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span></code></span></dt>
  266. <dd><p>
  267. arguments to <code class="computeroutput"><span class="identifier">fa</span></code>
  268. </p></dd>
  269. </dl>
  270. </div>
  271. <p>
  272. The result type of a target function invocation must be
  273. </p>
  274. <pre class="programlisting"><a href="http://www.boost.org/libs/utility/utility.htm#result_of" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">result_of</span></code></a><span class="special">&lt;</span><span class="identifier">F</span><span class="special">*(</span><span class="identifier">TA0</span> <span class="special">[</span><span class="keyword">const</span><span class="special">]&amp;...</span><span class="identifier">TAN</span> <span class="special">[</span><span class="keyword">const</span><span class="special">]&amp;])&gt;::</span><span class="identifier">type</span>
  275. </pre>
  276. <p>
  277. where <code class="computeroutput"><span class="identifier">TA0</span></code>...<code class="computeroutput"><span class="identifier">TAN</span></code> denote the argument types of <code class="computeroutput"><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span></code>.
  278. </p>
  279. <h5>
  280. <a name="boost_functional_forward.reference.forward_adapter.h3"></a>
  281. <span class="phrase"><a name="boost_functional_forward.reference.forward_adapter.expression_semantics"></a></span><a class="link" href="index.html#boost_functional_forward.reference.forward_adapter.expression_semantics">Expression
  282. Semantics</a>
  283. </h5>
  284. <div class="informaltable"><table class="table">
  285. <colgroup>
  286. <col>
  287. <col>
  288. </colgroup>
  289. <thead><tr>
  290. <th>
  291. <p>
  292. Expression
  293. </p>
  294. </th>
  295. <th>
  296. <p>
  297. Semantics
  298. </p>
  299. </th>
  300. </tr></thead>
  301. <tbody>
  302. <tr>
  303. <td>
  304. <p>
  305. <code class="computeroutput"><span class="identifier">FA</span><span class="special">(</span><span class="identifier">f</span><span class="special">)</span></code>
  306. </p>
  307. </td>
  308. <td>
  309. <p>
  310. creates an adapter, initializes the target function with <code class="computeroutput"><span class="identifier">f</span></code>.
  311. </p>
  312. </td>
  313. </tr>
  314. <tr>
  315. <td>
  316. <p>
  317. <code class="computeroutput"><span class="identifier">FA</span><span class="special">()</span></code>
  318. </p>
  319. </td>
  320. <td>
  321. <p>
  322. creates an adapter, attempts to use <code class="computeroutput"><span class="identifier">F</span></code>'s
  323. default constructor.
  324. </p>
  325. </td>
  326. </tr>
  327. <tr>
  328. <td>
  329. <p>
  330. <code class="computeroutput"><span class="identifier">fa</span><span class="special">(</span><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span><span class="special">)</span></code>
  331. </p>
  332. </td>
  333. <td>
  334. <p>
  335. calls <code class="computeroutput"><span class="identifier">f</span></code> with with
  336. arguments <code class="computeroutput"><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span></code>.
  337. </p>
  338. </td>
  339. </tr>
  340. </tbody>
  341. </table></div>
  342. <h5>
  343. <a name="boost_functional_forward.reference.forward_adapter.h4"></a>
  344. <span class="phrase"><a name="boost_functional_forward.reference.forward_adapter.limits"></a></span><a class="link" href="index.html#boost_functional_forward.reference.forward_adapter.limits">Limits</a>
  345. </h5>
  346. <p>
  347. The macro BOOST_FUNCTIONAL_FORWARD_ADAPTER_MAX_ARITY can be defined to set
  348. the maximum call arity. It defaults to 6.
  349. </p>
  350. <h5>
  351. <a name="boost_functional_forward.reference.forward_adapter.h5"></a>
  352. <span class="phrase"><a name="boost_functional_forward.reference.forward_adapter.complexity"></a></span><a class="link" href="index.html#boost_functional_forward.reference.forward_adapter.complexity">Complexity</a>
  353. </h5>
  354. <p>
  355. Preprocessing time: O(2^N), where N is the arity limit. Compile time: O(2^N),
  356. where N depends on the arity range. Run time: O(0) if the compiler inlines,
  357. O(1) otherwise.
  358. </p>
  359. </div>
  360. <div class="section">
  361. <div class="titlepage"><div><div><h3 class="title">
  362. <a name="boost_functional_forward.reference.lightweight_forward_adapter"></a><a class="link" href="index.html#boost_functional_forward.reference.lightweight_forward_adapter" title="lightweight_forward_adapter">lightweight_forward_adapter</a>
  363. </h3></div></div></div>
  364. <h5>
  365. <a name="boost_functional_forward.reference.lightweight_forward_adapter.h0"></a>
  366. <span class="phrase"><a name="boost_functional_forward.reference.lightweight_forward_adapter.description"></a></span><a class="link" href="index.html#boost_functional_forward.reference.lightweight_forward_adapter.description">Description</a>
  367. </h5>
  368. <p>
  369. Function object adapter template whose instances are callable with LValue
  370. and RValue arguments. All arguments are forwarded as reference-to-const typed
  371. LValues, except for reference wrappers which are unwrapped and may yield
  372. non-const LValues.
  373. </p>
  374. <p>
  375. An arity can be given as second, numeric non-type template argument to restrict
  376. forwarding to a specific arity. If a third, numeric non-type template argument
  377. is present, the second and third template argument are treated as minimum
  378. and maximum arity, respectively. Specifying an arity can be helpful to improve
  379. the readability of diagnostic messages and compile time performance.
  380. </p>
  381. <p>
  382. <a href="http://www.boost.org/libs/utility/utility.htm#result_of" target="_top">Boost.ResultOf</a>
  383. can be used to determine the result types of specific call expressions.
  384. </p>
  385. <h5>
  386. <a name="boost_functional_forward.reference.lightweight_forward_adapter.h1"></a>
  387. <span class="phrase"><a name="boost_functional_forward.reference.lightweight_forward_adapter.header"></a></span><a class="link" href="index.html#boost_functional_forward.reference.lightweight_forward_adapter.header">Header</a>
  388. </h5>
  389. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">functional</span><span class="special">/</span><span class="identifier">lightweight_forward_adapter</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  390. </pre>
  391. <h5>
  392. <a name="boost_functional_forward.reference.lightweight_forward_adapter.h2"></a>
  393. <span class="phrase"><a name="boost_functional_forward.reference.lightweight_forward_adapter.synopsis"></a></span><a class="link" href="index.html#boost_functional_forward.reference.lightweight_forward_adapter.synopsis">Synopsis</a>
  394. </h5>
  395. <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span>
  396. <span class="special">{</span>
  397. <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">Function</span><span class="special">,</span>
  398. <span class="keyword">int</span> <span class="identifier">Arity_Or_MinArity</span> <span class="special">=</span> <span class="emphasis"><em>unspecified</em></span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">MaxArity</span> <span class="special">=</span> <span class="emphasis"><em>unspecified</em></span> <span class="special">&gt;</span>
  399. <span class="keyword">struct</span> <span class="identifier">lightweight_forward_adapter</span><span class="special">;</span>
  400. <span class="special">}</span>
  401. </pre>
  402. <div class="variablelist">
  403. <p class="title"><b>Notation</b></p>
  404. <dl class="variablelist">
  405. <dt><span class="term"><code class="computeroutput"><span class="identifier">F</span></code></span></dt>
  406. <dd><p>
  407. a possibly const qualified function object type or reference type thereof
  408. </p></dd>
  409. <dt><span class="term"><code class="computeroutput"><span class="identifier">f</span></code></span></dt>
  410. <dd><p>
  411. an object convertible to <code class="computeroutput"><span class="identifier">F</span></code>
  412. </p></dd>
  413. <dt><span class="term"><code class="computeroutput"><span class="identifier">FA</span></code></span></dt>
  414. <dd><p>
  415. the type <code class="computeroutput"><span class="identifier">lightweight_forward_adapter</span><span class="special">&lt;</span><span class="identifier">F</span><span class="special">&gt;</span></code>
  416. </p></dd>
  417. <dt><span class="term"><code class="computeroutput"><span class="identifier">fa</span></code></span></dt>
  418. <dd><p>
  419. an instance of <code class="computeroutput"><span class="identifier">FA</span></code>,
  420. initialized with <code class="computeroutput"><span class="identifier">f</span></code>
  421. </p></dd>
  422. <dt><span class="term"><code class="computeroutput"><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span></code></span></dt>
  423. <dd><p>
  424. arguments to <code class="computeroutput"><span class="identifier">fa</span></code>
  425. </p></dd>
  426. </dl>
  427. </div>
  428. <p>
  429. The result type of a target function invocation must be
  430. </p>
  431. <pre class="programlisting"><a href="http://www.boost.org/libs/utility/utility.htm#result_of" target="_top"><code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">result_of</span></code></a><span class="special">&lt;</span><span class="identifier">F</span><span class="special">*(</span><span class="identifier">TA0</span> <span class="special">[</span><span class="keyword">const</span><span class="special">]&amp;...</span><span class="identifier">TAN</span> <span class="special">[</span><span class="keyword">const</span><span class="special">]&amp;])&gt;::</span><span class="identifier">type</span>
  432. </pre>
  433. <p>
  434. where <code class="computeroutput"><span class="identifier">TA0</span></code>...<code class="computeroutput"><span class="identifier">TAN</span></code> denote the argument types of <code class="computeroutput"><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span></code>.
  435. </p>
  436. <h5>
  437. <a name="boost_functional_forward.reference.lightweight_forward_adapter.h3"></a>
  438. <span class="phrase"><a name="boost_functional_forward.reference.lightweight_forward_adapter.expression_semantics"></a></span><a class="link" href="index.html#boost_functional_forward.reference.lightweight_forward_adapter.expression_semantics">Expression
  439. Semantics</a>
  440. </h5>
  441. <div class="informaltable"><table class="table">
  442. <colgroup>
  443. <col>
  444. <col>
  445. </colgroup>
  446. <thead><tr>
  447. <th>
  448. <p>
  449. Expression
  450. </p>
  451. </th>
  452. <th>
  453. <p>
  454. Semantics
  455. </p>
  456. </th>
  457. </tr></thead>
  458. <tbody>
  459. <tr>
  460. <td>
  461. <p>
  462. <code class="computeroutput"><span class="identifier">FA</span><span class="special">(</span><span class="identifier">f</span><span class="special">)</span></code>
  463. </p>
  464. </td>
  465. <td>
  466. <p>
  467. creates an adapter, initializes the target function with <code class="computeroutput"><span class="identifier">f</span></code>.
  468. </p>
  469. </td>
  470. </tr>
  471. <tr>
  472. <td>
  473. <p>
  474. <code class="computeroutput"><span class="identifier">FA</span><span class="special">()</span></code>
  475. </p>
  476. </td>
  477. <td>
  478. <p>
  479. creates an adapter, attempts to use <code class="computeroutput"><span class="identifier">F</span></code>'s
  480. default constructor.
  481. </p>
  482. </td>
  483. </tr>
  484. <tr>
  485. <td>
  486. <p>
  487. <code class="computeroutput"><span class="identifier">fa</span><span class="special">(</span><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span><span class="special">)</span></code>
  488. </p>
  489. </td>
  490. <td>
  491. <p>
  492. calls <code class="computeroutput"><span class="identifier">f</span></code> with with
  493. const arguments <code class="computeroutput"><span class="identifier">a0</span></code>...<code class="computeroutput"><span class="identifier">aN</span></code>. If <code class="computeroutput"><span class="identifier">aI</span></code>
  494. is a reference wrapper it is unwrapped.
  495. </p>
  496. </td>
  497. </tr>
  498. </tbody>
  499. </table></div>
  500. <h5>
  501. <a name="boost_functional_forward.reference.lightweight_forward_adapter.h4"></a>
  502. <span class="phrase"><a name="boost_functional_forward.reference.lightweight_forward_adapter.limits"></a></span><a class="link" href="index.html#boost_functional_forward.reference.lightweight_forward_adapter.limits">Limits</a>
  503. </h5>
  504. <p>
  505. The macro BOOST_FUNCTIONAL_LIGHTWEIGHT_FORWARD_ADAPTER_MAX_ARITY can be defined
  506. to set the maximum call arity. It defaults to 10.
  507. </p>
  508. <h5>
  509. <a name="boost_functional_forward.reference.lightweight_forward_adapter.h5"></a>
  510. <span class="phrase"><a name="boost_functional_forward.reference.lightweight_forward_adapter.complexity"></a></span><a class="link" href="index.html#boost_functional_forward.reference.lightweight_forward_adapter.complexity">Complexity</a>
  511. </h5>
  512. <p>
  513. Preprocessing time: O(N), where N is the arity limit. Compile time: O(N),
  514. where N is the effective arity of a call. Run time: O(0) if the compiler
  515. inlines, O(1) otherwise.
  516. </p>
  517. </div>
  518. </div>
  519. <div class="section">
  520. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  521. <a name="boost_functional_forward.acknowledgements"></a><a class="link" href="index.html#boost_functional_forward.acknowledgements" title="Acknowledgements">Acknowledgements</a>
  522. </h2></div></div></div>
  523. <p>
  524. As these utilities are factored out of the <a href="http://www.boost.org/libs/fusion/doc/html/index.html" target="_top">Boost.Fusion</a>
  525. functional module, I want to thank Dan Marsden and Joel de Guzman for letting
  526. me participate in the development of that great library in the first place.
  527. </p>
  528. <p>
  529. Further, I want to credit the authors of the references below, for their in-depth
  530. investigation of the problem and the solution implemented here.
  531. </p>
  532. <p>
  533. Last but not least I want to thank Vesa Karnoven and Paul Mensonides for the
  534. Boost Preprocessor library. Without it, I would have ended up with an external
  535. code generator for this one.
  536. </p>
  537. </div>
  538. <div class="section">
  539. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  540. <a name="boost_functional_forward.references"></a><a class="link" href="index.html#boost_functional_forward.references" title="References">References</a>
  541. </h2></div></div></div>
  542. <div class="orderedlist"><ol class="orderedlist" type="1">
  543. <li class="listitem">
  544. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1385.htm" target="_top">The
  545. Forwarding Problem</a>, Peter Dimov, Howard E. Hinnant, David Abrahams,
  546. 2002
  547. </li>
  548. <li class="listitem">
  549. <a href="http://www.boost.org/libs/utility/utility.htm#result_of" target="_top">Boost.ResultOf</a>,
  550. Douglas Gregor, 2004
  551. </li>
  552. <li class="listitem">
  553. <a href="http://www.boost.org/doc/html/ref.html" target="_top">Boost.Ref</a>,
  554. Jaakko Jarvi, Peter Dimov, Douglas Gregor, David Abrahams, 1999-2002
  555. </li>
  556. </ol></div>
  557. </div>
  558. </div>
  559. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  560. <td align="left"><p><small>Last revised: November 01, 2008 at 19:58:50 GMT</small></p></td>
  561. <td align="right"><div class="copyright-footer"></div></td>
  562. </tr></table>
  563. <hr>
  564. <div class="spirit-nav"></div>
  565. </body>
  566. </html>