]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Lazy Functions</title> | |
5 | <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> | |
7 | <link rel="home" href="../../index.html" title="Chapter 1. Phoenix 3.2.0"> | |
8 | <link rel="up" href="../starter_kit.html" title="Starter Kit"> | |
9 | <link rel="prev" href="construct__new__delete__casts.html" title="Construct, New, Delete, Casts"> | |
10 | <link rel="next" href="more.html" title="More"> | |
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="construct__new__delete__casts.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../starter_kit.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="more.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.starter_kit.lazy_functions"></a><a class="link" href="lazy_functions.html" title="Lazy Functions">Lazy Functions</a> | |
28 | </h3></div></div></div> | |
29 | <p> | |
30 | As you write more lambda functions, you'll notice certain patterns that you | |
31 | wish to refactor as reusable functions. When you reach that point, you'll | |
32 | wish that ordinary functions can co-exist with phoenix functions. Unfortunately, | |
33 | the <span class="emphasis"><em>immediate</em></span> nature of plain C++ functions make them | |
34 | incompatible. | |
35 | </p> | |
36 | <p> | |
37 | Lazy functions are your friends. The library provides a facility to make | |
38 | lazy functions. The code below is a rewrite of the <code class="computeroutput"><span class="identifier">is_odd</span></code> | |
39 | function using the facility: | |
40 | </p> | |
41 | <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">is_odd_impl</span> | |
42 | <span class="special">{</span> | |
43 | <span class="keyword">typedef</span> <span class="keyword">bool</span> <span class="identifier">result_type</span><span class="special">;</span> | |
44 | ||
45 | <span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">Arg</span><span class="special">></span> | |
46 | <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">Arg</span> <span class="identifier">arg1</span><span class="special">)</span> <span class="keyword">const</span> | |
47 | <span class="special">{</span> | |
48 | <span class="keyword">return</span> <span class="identifier">arg1</span> <span class="special">%</span> <span class="number">2</span> <span class="special">==</span> <span class="number">1</span><span class="special">;</span> | |
49 | <span class="special">}</span> | |
50 | <span class="special">};</span> | |
51 | ||
52 | <span class="identifier">function</span><span class="special"><</span><span class="identifier">is_odd_impl</span><span class="special">></span> <span class="identifier">is_odd</span><span class="special">;</span> | |
53 | </pre> | |
54 | <h5> | |
55 | <a name="phoenix.starter_kit.lazy_functions.h0"></a> | |
56 | <span><a name="phoenix.starter_kit.lazy_functions.things_to_note_"></a></span><a class="link" href="lazy_functions.html#phoenix.starter_kit.lazy_functions.things_to_note_">Things | |
57 | to note:</a> | |
58 | </h5> | |
59 | <div class="itemizedlist"><ul class="itemizedlist" type="disc"> | |
60 | <li class="listitem"> | |
61 | Result type deduction is implemented with the help of the result_of protocol. | |
62 | For more information see <a href="http://www.boost.org/doc/libs/release/libs/utility/utility.htm#result_of" target="_top">Boost.Result | |
63 | Of</a> | |
64 | </li> | |
65 | <li class="listitem"> | |
66 | <code class="computeroutput"><span class="identifier">is_odd_impl</span></code> implements | |
67 | the function. | |
68 | </li> | |
69 | <li class="listitem"> | |
70 | <code class="computeroutput"><span class="identifier">is_odd</span></code>, an instance of | |
71 | <code class="computeroutput"><span class="identifier">function</span><span class="special"><</span><span class="identifier">is_odd_impl</span><span class="special">></span></code>, | |
72 | is the lazy function. | |
73 | </li> | |
74 | </ul></div> | |
75 | <p> | |
76 | Now, <code class="computeroutput"><span class="identifier">is_odd</span></code> is a truly lazy | |
77 | function that we can use in conjunction with the rest of phoenix. Example: | |
78 | </p> | |
79 | <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">find_if</span><span class="special">(</span><span class="identifier">c</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">c</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">is_odd</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">));</span> | |
80 | </pre> | |
81 | <p> | |
82 | (See <a href="../../../../example/function.cpp" target="_top">function.cpp</a>) | |
83 | </p> | |
84 | <h5> | |
85 | <a name="phoenix.starter_kit.lazy_functions.h1"></a> | |
86 | <span><a name="phoenix.starter_kit.lazy_functions.predefined_lazy_functions"></a></span><a class="link" href="lazy_functions.html#phoenix.starter_kit.lazy_functions.predefined_lazy_functions">Predefined | |
87 | Lazy Functions</a> | |
88 | </h5> | |
89 | <p> | |
90 | The library is chock full of STL savvy, predefined lazy functions covering | |
91 | the whole of the STL containers, iterators and algorithms. For example, there | |
92 | are lazy versions of container related operations such as assign, at, back, | |
93 | begin, pop_back, pop_front, push_back, push_front, etc. (See <a class="link" href="../modules/stl.html" title="STL">STL</a>). | |
94 | </p> | |
95 | </div> | |
96 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
97 | <td align="left"></td> | |
98 | <td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2010, 2014, 2015 Joel de Guzman, Dan Marsden, Thomas | |
99 | Heller, John Fletcher<p> | |
100 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
101 | 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>) | |
102 | </p> | |
103 | </div></td> | |
104 | </tr></table> | |
105 | <hr> | |
106 | <div class="spirit-nav"> | |
107 | <a accesskey="p" href="construct__new__delete__casts.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../starter_kit.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="more.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> | |
108 | </div> | |
109 | </body> | |
110 | </html> |