]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/phoenix/doc/starter_kit/function.qbk
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / phoenix / doc / starter_kit / function.qbk
1 [/==============================================================================
2 Copyright (C) 2001-2010 Joel de Guzman
3 Copyright (C) 2001-2005 Dan Marsden
4 Copyright (C) 2001-2010 Thomas Heller
5
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 ===============================================================================/]
9
10 [section Lazy Functions]
11
12 As you write more lambda functions, you'll notice certain patterns that you wish
13 to refactor as reusable functions. When you reach that point, you'll wish that
14 ordinary functions can co-exist with phoenix functions. Unfortunately, the
15 /immediate/ nature of plain C++ functions make them incompatible.
16
17 Lazy functions are your friends. The library provides a facility to make lazy
18 functions. The code below is a rewrite of the `is_odd` function using the
19 facility:
20
21 struct is_odd_impl
22 {
23 typedef bool result_type;
24
25 template <typename Arg>
26 bool operator()(Arg arg1) const
27 {
28 return arg1 % 2 == 1;
29 }
30 };
31
32 function<is_odd_impl> is_odd;
33
34 [heading Things to note:]
35
36 [/
37 * `result` is a nested metafunction that reflects the return type of the
38 function (in this case, bool). This makes the function fully polymorphic:
39 It can work with arbitrary `Arg` types.
40 * There are as many Args in the `result` metafunction as in the actual
41 `operator()`.
42 ]
43 * Result type deduction is implemented with the help of the result_of protocol.
44 For more information see __boost_result_of__
45 * `is_odd_impl` implements the function.
46 * `is_odd`, an instance of `function<is_odd_impl>`, is the lazy function.
47
48 Now, `is_odd` is a truly lazy function that we can use in conjunction with the
49 rest of phoenix. Example:
50
51 std::find_if(c.begin(), c.end(), is_odd(arg1));
52
53 (See [@../../example/function.cpp function.cpp])
54
55 [heading Predefined Lazy Functions]
56
57 The library is chock full of STL savvy, predefined lazy functions covering the
58 whole of the STL containers, iterators and algorithms. For example, there are lazy
59 versions of container related operations such as assign, at, back, begin,
60 pop_back, pop_front, push_back, push_front, etc. (See [link phoenix.modules.stl
61 STL]).
62
63 [endsect]