Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/spirit/phoenix/doc/functions.html @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 7.7 KB
Line 
1<html>
2<head>
3<!-- Generated by the Spirit (http://spirit.sf.net) QuickDoc -->
4<title>Functions</title>
5<link rel="stylesheet" href="theme/style.css" type="text/css">
6<link rel="prev" href="composites.html">
7<link rel="next" href="operators.html">
8</head>
9<body>
10<table width="100%" height="48" border="0" background="theme/bkd2.gif" cellspacing="2">
11  <tr>
12    <td width="10">
13    </td>
14    <td width="85%">
15      <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Functions</b></font>
16    </td>
17    <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" align="right" border="0"></a></td>
18  </tr>
19</table>
20<br>
21<table border="0">
22  <tr>
23    <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
24    <td width="30"><a href="composites.html"><img src="theme/l_arr.gif" border="0"></a></td>
25    <td width="20"><a href="operators.html"><img src="theme/r_arr.gif" border="0"></a></td>
26   </tr>
27</table>
28<a name="lazy_functions"></a><h2>Lazy functions</h2><p>
29This class provides a mechanism for lazily evaluating functions. Syntactically, a lazy function looks like an ordinary C/C++ function. The function call looks familiar and feels the same as ordinary C++ functions. However, unlike ordinary functions, the actual function execution is deferred. For example here are sample factorial function calls:</p>
30<code><pre>
31    <span class=identifier>factorial</span><span class=special>(</span><span class=number>4</span><span class=special>)
32    </span><span class=identifier>factorial</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>)
33    </span><span class=identifier>factorial</span><span class=special>(</span><span class=identifier>arg1 </span><span class=special>* </span><span class=number>6 </span><span class=special>/ </span><span class=identifier>factorial</span><span class=special>(</span><span class=identifier>var</span><span class=special>(</span><span class=identifier>i</span><span class=special>)))
34</span></pre></code>
35<p>
36These functions are automatically lazily bound unlike ordinary function pointers or functor objects that need to be explicitly bound through the bind function (see binders).</p>
37<p>
38A lazy function works in conjunction with a user defined functor (as usual with a member operator()). Only special forms of functor objects are allowed. This is required to enable true polymorphism (STL style monomorphic functors and function pointers can still be used through the bind facility (see binders)).</p>
39<p>
40This special functor is expected to have a nested template class result&lt;T0...TN&gt; (where N is the number of arguments of its member operator()). The nested template class result should have a typedef 'type' that reflects the return type of its member operator(). This is essentially a type computer that answers the metaprogramming question &quot;Given arguments of type T0...TN, what will be the functor operator()'s return type?&quot;.</p>
41<p>
42There is a special case for functors that accept no arguments. Such nullary functors are only required to define a typedef result_type that reflects the return type of its operator().</p>
43<p>
44Here's an example of a simple functor that computes the factorial of a number:</p>
45<code><pre>
46    <span class=keyword>struct </span><span class=identifier>factorial_impl </span><span class=special>{
47
48        </span><span class=keyword>template </span><span class=special>&lt;</span><span class=keyword>typename </span><span class=identifier>Arg</span><span class=special>&gt;
49        </span><span class=keyword>struct </span><span class=identifier>result </span><span class=special>{ </span><span class=keyword>typedef </span><span class=identifier>Arg </span><span class=identifier>type</span><span class=special>; };
50
51        </span><span class=keyword>template </span><span class=special>&lt;</span><span class=keyword>typename </span><span class=identifier>Arg</span><span class=special>&gt;
52        </span><span class=identifier>Arg </span><span class=keyword>operator</span><span class=special>()(</span><span class=identifier>Arg </span><span class=identifier>n</span><span class=special>) </span><span class=keyword>const
53        </span><span class=special>{ </span><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=number>1 </span><span class=special>: </span><span class=identifier>n </span><span class=special>* </span><span class=keyword>this</span><span class=special>-&gt;</span><span class=keyword>operator</span><span class=special>()(</span><span class=identifier>n</span><span class=special>-</span><span class=number>1</span><span class=special>); }
54    };
55</span></pre></code>
56<p>
57As can be seen, the functor is polymorphic. Its arguments and return type are not fixed to a particular type. The example above for example, can handle any type as long as it can carry out the required operations (i.e. &lt;=, * and -).</p>
58<p>
59We can now declare and instantiate a lazy 'factorial' function:</p>
60<code><pre>
61    <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>;
62</span></pre></code>
63<p>
64Invoking a lazy function 'factorial' does not immediately execute the functor factorial_impl. Instead, a composite object is created and returned to the caller. Example:</p>
65<code><pre>
66    <span class=identifier>factorial</span><span class=special>(</span><span class=identifier>arg1</span><span class=special>)
67</span></pre></code>
68<p>
69does nothing more than return a composite. A second function call will invoke the actual factorial function. Example:</p>
70<code><pre>
71    <span class=keyword>int </span><span class=identifier>i </span><span class=special>= </span><span class=number>4</span><span class=special>;
72    </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=identifier>i</span><span class=special>);
73</span></pre></code>
74<p>
75will print out &quot;24&quot;.</p>
76<p>
77Take note that in certain cases (e.g. for functors with state), an instance of the functor may be passed on to the constructor. Example:</p>
78<code><pre>
79    <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>);
80</span></pre></code>
81<p>
82where ftor is an instance of factorial_impl (this is not necessary in this case since factorial is a simple stateless functor). Take care though when using functors with state because the functors are taken in by value. It is best to keep the data manipulated by a functor outside the functor itself and keep a reference to this data inside the functor. Also, it is best to keep functors as small as possible.</p>
83<table border="0">
84  <tr>
85    <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
86    <td width="30"><a href="composites.html"><img src="theme/l_arr.gif" border="0"></a></td>
87    <td width="20"><a href="operators.html"><img src="theme/r_arr.gif" border="0"></a></td>
88   </tr>
89</table>
90<br>
91<hr size="1">
92<p class="copyright">Copyright &copy; 2001-2002 Joel de Guzman<br>
93  <br>
94<font size="2">Use, modification and distribution is subject to the Boost Software
95    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
96    http://www.boost.org/LICENSE_1_0.txt) </font> </p>
97</body>
98</html>
Note: See TracBrowser for help on using the repository browser.