summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/phoenix/example/lambda.cpp
blob: adfd798e7d283ae71d0978c75c2f8e278bdb269f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*=============================================================================
    Copyright (c) 2001-2007 Joel de Guzman

    Distributed under the Boost Software License, Version 1.0. (See accompanying 
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#include <iostream>
#include <algorithm>
#include <vector>

#include <boost/phoenix/scope.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/function.hpp>

namespace lazy_stuff
{
    using boost::phoenix::function;

    struct for_each_impl
    {
        typedef void result_type;

        template <typename C, typename F>
        void operator()(C& c, F f) const
        {
            std::for_each(c.begin(), c.end(), f);
        }
    };

    function<for_each_impl> const for_each = for_each_impl();

    struct push_back_impl
    {
        typedef void result_type;

        template <typename C, typename T>
        void operator()(C& c, T& x) const
        {
            c.push_back(x);
        }
    };

    function<push_back_impl> const push_back = push_back_impl();
}

int
main()
{
    {
        using lazy_stuff::for_each;
        using lazy_stuff::push_back;

        using boost::phoenix::lambda;
        using boost::phoenix::arg_names::arg1;
        using boost::phoenix::arg_names::arg2;
        using boost::phoenix::local_names::_a;

        int x = 10;
        std::vector<std::vector<int> > v(10);

        for_each(arg1, 
            lambda(_a = arg2)
            [
                push_back(arg1, _a)
            ]
        )
        (v, x);
    }

    return 0;
}