/Src/Dependencies/Boost/libs/phoenix/doc/html/phoenix/modules/function.html

http://hadesmem.googlecode.com/ · HTML · 150 lines · 148 code · 2 blank · 0 comment · 0 complexity · 126a6b8e815421a0e5460f8ea3abd84f MD5 · raw file

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Function</title>
  5. <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
  7. <link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Phoenix 3.0">
  8. <link rel="up" href="../modules.html" title="Modules">
  9. <link rel="prev" href="core/nothing.html" title="Nothing">
  10. <link rel="next" href="function/adapting_functions.html" title="Adapting Functions">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr>
  14. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
  15. <td align="center"><a href="../../../../../../index.html">Home</a></td>
  16. <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
  17. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  18. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  19. <td align="center"><a href="../../../../../../more/index.htm">More</a></td>
  20. </tr></table>
  21. <hr>
  22. <div class="spirit-nav">
  23. <a accesskey="p" href="core/nothing.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../modules.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="function/adapting_functions.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h3 class="title">
  27. <a name="phoenix.modules.function"></a><a class="link" href="function.html" title="Function">Function</a>
  28. </h3></div></div></div>
  29. <div class="toc"><dl><dt><span class="section"><a href="function/adapting_functions.html">Adapting
  30. Functions</a></span></dt></dl></div>
  31. <p>
  32. The <code class="computeroutput"><span class="identifier">function</span></code> class template
  33. provides a mechanism for implementing lazily evaluated functions. Syntactically,
  34. a lazy function looks like an ordinary C/C++ function. The function call
  35. looks familiar and feels the same as ordinary C++ functions. However, unlike
  36. ordinary functions, the actual function execution is deferred.
  37. </p>
  38. <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">phoenix</span><span class="special">/</span><span class="identifier">function</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  39. </pre>
  40. <p>
  41. Unlike ordinary function pointers or functor objects that need to be explicitly
  42. bound through the bind function (see <a class="link" href="bind.html" title="Bind">Bind</a>),
  43. the argument types of these functions are automatically lazily bound.
  44. </p>
  45. <p>
  46. In order to create a lazy function, we need to implement a model of the
  47. <a href="http://www.boost.org/doc/libs/release/libs/fusion/doc/html/fusion/functional/concepts/poly.html" target="_top">Polymorphic
  48. Function Object</a> concept. For a function that takes <code class="computeroutput"><span class="identifier">N</span></code> arguments, a model of <a href="http://www.boost.org/doc/libs/release/libs/fusion/doc/html/fusion/functional/concepts/poly.html" target="_top">Polymorphic
  49. Function Object</a> must provide:
  50. </p>
  51. <div class="itemizedlist"><ul class="itemizedlist" type="disc">
  52. <li class="listitem">
  53. An <code class="computeroutput"><span class="keyword">operator</span><span class="special">()</span></code>
  54. that takes <code class="computeroutput"><span class="identifier">N</span></code> arguments,
  55. and implements the function logic. This is also true for ordinary function
  56. pointers.
  57. </li>
  58. <li class="listitem">
  59. A nested metafunction <code class="computeroutput"><span class="identifier">result</span><span class="special">&lt;</span><span class="identifier">Signature</span><span class="special">&gt;</span></code> or nested typedef <code class="computeroutput"><span class="identifier">result_type</span></code>,
  60. following the <a href="http://www.boost.org/doc/libs/release/libs/utility/utility.htm#result_of" target="_top">Boost.Result
  61. Of</a> Protocol
  62. </li>
  63. </ul></div>
  64. <p>
  65. For example, the following type implements the FunctionEval concept, in order
  66. to provide a lazy factorial function:
  67. </p>
  68. <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">factorial_impl</span>
  69. <span class="special">{</span>
  70. <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Sig</span><span class="special">&gt;</span>
  71. <span class="keyword">struct</span> <span class="identifier">result</span><span class="special">;</span>
  72. <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">This</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Arg</span><span class="special">&gt;</span>
  73. <span class="keyword">struct</span> <span class="identifier">result</span><span class="special">&lt;</span><span class="identifier">This</span><span class="special">(</span><span class="identifier">Arg</span> <span class="keyword">const</span> <span class="special">&amp;)&gt;</span>
  74. <span class="special">{</span>
  75. <span class="keyword">typedef</span> <span class="identifier">Arg</span> <span class="identifier">type</span><span class="special">;</span>
  76. <span class="special">};</span>
  77. <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Arg</span><span class="special">&gt;</span>
  78. <span class="identifier">Arg</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">Arg</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">n</span><span class="special">)</span> <span class="keyword">const</span>
  79. <span class="special">{</span>
  80. <span class="keyword">return</span> <span class="special">(</span><span class="identifier">n</span> <span class="special">&lt;=</span> <span class="number">0</span><span class="special">)</span> <span class="special">?</span> <span class="number">1</span> <span class="special">:</span> <span class="identifier">n</span> <span class="special">*</span> <span class="special">(*</span><span class="keyword">this</span><span class="special">)(</span><span class="identifier">n</span><span class="special">-</span><span class="number">1</span><span class="special">);</span>
  81. <span class="special">}</span>
  82. <span class="special">};</span>
  83. </pre>
  84. <p>
  85. (See <a href="../../../../example/factorial.cpp" target="_top">factorial.cpp</a>)
  86. </p>
  87. <p>
  88. Having implemented the <code class="computeroutput"><span class="identifier">factorial_impl</span></code>
  89. type, we can declare and instantiate a lazy <code class="computeroutput"><span class="identifier">factorial</span></code>
  90. function this way:
  91. </p>
  92. <pre class="programlisting"><span class="identifier">function</span><span class="special">&lt;</span><span class="identifier">factorial_impl</span><span class="special">&gt;</span> <span class="identifier">factorial</span><span class="special">;</span>
  93. </pre>
  94. <p>
  95. Invoking a lazy function such as <code class="computeroutput"><span class="identifier">factorial</span></code>
  96. does not immediately execute the function object <code class="computeroutput"><span class="identifier">factorial_impl</span></code>.
  97. Instead, an <a class="link" href="../actor.html" title="Actor">actor</a> object is created
  98. and returned to the caller. Example:
  99. </p>
  100. <pre class="programlisting"><span class="identifier">factorial</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">)</span>
  101. </pre>
  102. <p>
  103. does nothing more than return an actor. A second function call will invoke
  104. the actual factorial function. Example:
  105. </p>
  106. <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">factorial</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">)(</span><span class="number">4</span><span class="special">);</span>
  107. </pre>
  108. <p>
  109. will print out "24".
  110. </p>
  111. <p>
  112. Take note that in certain cases (e.g. for function objects with state), an
  113. instance of the model of <a href="http://www.boost.org/doc/libs/release/libs/fusion/doc/html/fusion/functional/concepts/poly.html" target="_top">Polymorphic
  114. Function Object</a> may be passed on to the constructor. Example:
  115. </p>
  116. <pre class="programlisting"><span class="identifier">function</span><span class="special">&lt;</span><span class="identifier">factorial_impl</span><span class="special">&gt;</span> <span class="identifier">factorial</span><span class="special">(</span><span class="identifier">ftor</span><span class="special">);</span>
  117. </pre>
  118. <p>
  119. where ftor is an instance of factorial_impl (this is not necessary in this
  120. case as <code class="computeroutput"><span class="identifier">factorial_impl</span></code> does
  121. not require any state).
  122. </p>
  123. <div class="important"><table border="0" summary="Important">
  124. <tr>
  125. <td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../../doc/src/images/important.png"></td>
  126. <th align="left">Important</th>
  127. </tr>
  128. <tr><td align="left" valign="top"><p>
  129. Take care though when using function objects with state because they are
  130. often copied repeatedly, and state may change in one of the copies, rather
  131. than the original.
  132. </p></td></tr>
  133. </table></div>
  134. </div>
  135. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  136. <td align="left"></td>
  137. <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005, 2010 Joel de Guzman, Dan Marsden, Thomas Heller<p>
  138. Distributed under the Boost Software License, Version 1.0. (See accompanying
  139. 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>)
  140. </p>
  141. </div></td>
  142. </tr></table>
  143. <hr>
  144. <div class="spirit-nav">
  145. <a accesskey="p" href="core/nothing.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../modules.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="function/adapting_functions.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  146. </div>
  147. </body>
  148. </html>