• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2007 Joel de Guzman
3 
4     Distributed under the Boost Software License, Version 1.0. (See accompanying
5     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #include <iostream>
8 #include <cmath>
9 #include <algorithm>
10 #include <vector>
11 
12 #include <boost/detail/lightweight_test.hpp>
13 #include <boost/phoenix/core.hpp>
14 #include <boost/phoenix/operator.hpp>
15 #include <boost/phoenix/scope.hpp>
16 #include <boost/phoenix/function.hpp>
17 
18 namespace boost { namespace phoenix
19 {
20     struct for_each_impl
21     {
22         template <typename C, typename F>
23         struct result
24         {
25             typedef void type;
26         };
27 
28         template <typename C, typename F>
operator ()boost::phoenix::for_each_impl29         void operator()(C& c, F f) const
30         {
31             std::for_each(c.begin(), c.end(), f);
32         }
33     };
34 
35     function<for_each_impl> const for_each = for_each_impl();
36 
37     struct push_back_impl
38     {
39         template <typename C, typename T>
40         struct result
41         {
42             typedef void type;
43         };
44 
45         template <typename C, typename T>
operator ()boost::phoenix::push_back_impl46         void operator()(C& c, T& x) const
47         {
48             c.push_back(x);
49         }
50     };
51 
52     function<push_back_impl> const push_back = push_back_impl();
53 }}
54 
55 using namespace boost::phoenix;
56 using namespace boost::phoenix::arg_names;
57 using namespace boost::phoenix::local_names;
58 using namespace std;
59 
60 struct zzz {};
61 
62 int
main()63 main()
64 {
65     {
66         using boost::phoenix::for_each;
67 
68         int init[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
69         std::vector<int> v(init, init+10);
70 
71         int x = 0;
72         for_each(_1, lambda(_a = _2)[_a += _1])(v, x);
73         BOOST_TEST(x == 55);
74     }
75 
76     {
77         using boost::phoenix::for_each;
78         using boost::phoenix::push_back;
79 
80         int x = 10;
81         std::vector<std::vector<int> > v(10);
82 
83         for_each(_1, lambda(_a = _2)[push_back(_1, _a)])(v, x);
84 
85         int y = 0;
86         for_each(arg1, lambda[ref(y) += _1[0]])(v);
87         BOOST_TEST(y == 100);
88     }
89     return boost::report_errors();
90 }
91 
92