summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/phoenix/example/define_expression.cpp
blob: f2744bd75141635a07ee749f3a9078ec8229bc89 (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
/*==============================================================================
    Copyright (c) 2005-2010 Joel de Guzman
    Copyright (c) 2011 Thomas Heller

    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 <boost/phoenix/core.hpp>

namespace phoenix = boost::phoenix;
namespace proto   = boost::proto;


// define the expression
namespace expression
{
    template <typename Lhs, typename Rhs>
    struct plus
        : phoenix::expr<proto::tag::plus, Lhs, Rhs>
    {};
}

// extend the grammar, to recognice the expression
namespace boost { namespace phoenix {

    template <>
    struct meta_grammar::case_<proto::tag::plus>
        : enable_rule<
            ::expression::plus<
                meta_grammar
              , meta_grammar
            >
        >
    {};

}}

// build a generator
template <typename Lhs, typename Rhs>
typename expression::plus<Lhs, Rhs>::type
plus(Lhs const & lhs, Rhs const & rhs)
{
    return expression::plus<Lhs, Rhs>::make(lhs, rhs);
}

#include <boost/proto/proto.hpp>
#include <iostream>

int main()
{

    plus(6, 5);

    proto::display_expr(plus(6, 5));

    std::cout << plus(5, 6)() << "\n";
}