define_expression.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*==============================================================================
  2. Copyright (c) 2005-2010 Joel de Guzman
  3. Copyright (c) 2011 Thomas Heller
  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 <boost/phoenix/core.hpp>
  8. namespace phoenix = boost::phoenix;
  9. namespace proto = boost::proto;
  10. // define the expression
  11. namespace expression
  12. {
  13. template <typename Lhs, typename Rhs>
  14. struct plus
  15. : phoenix::expr<proto::tag::plus, Lhs, Rhs>
  16. {};
  17. }
  18. // extend the grammar, to recognice the expression
  19. namespace boost { namespace phoenix {
  20. template <>
  21. struct meta_grammar::case_<proto::tag::plus>
  22. : enable_rule<
  23. ::expression::plus<
  24. meta_grammar
  25. , meta_grammar
  26. >
  27. >
  28. {};
  29. }}
  30. // build a generator
  31. template <typename Lhs, typename Rhs>
  32. typename expression::plus<Lhs, Rhs>::type
  33. plus(Lhs const & lhs, Rhs const & rhs)
  34. {
  35. return expression::plus<Lhs, Rhs>::make(lhs, rhs);
  36. }
  37. #include <boost/proto/proto.hpp>
  38. #include <iostream>
  39. int main()
  40. {
  41. plus(6, 5);
  42. proto::display_expr(plus(6, 5));
  43. std::cout << plus(5, 6)() << "\n";
  44. }