转换Boost C ++ Phoenix表达式树 [英] Transforming a Boost C++ Phoenix Expression Tree

查看:273
本文介绍了转换Boost C ++ Phoenix表达式树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Boost Phoenix文章转换表达式树中,这里,自定义 invert_actions 类的一组特化用于反转二进制算术表达式。例如 a + b 变为 a-b ; a * b 变为 a / b ;

In the Boost Phoenix article, "Transforming the Expression Tree", here, a set of specialisations of a custom invert_actions class, are used to invert binary arithmetic expressions. For example a+b becomes a-b; a*b becomes a/b; and vice versa for both.

这涉及表达式树的递归遍历 - 但是,遇到涉及未明确处理的运算符的表达式时,此遍历​​停止。例如, _1 + _2-_3 将变为 _1-_2 + _3 ,但 _1 + _1& _2 将保持原样(没有& 的处理程序)。 let(_a = 1,_b = 2)[_a + _b] 将保持不变。

This involves a recursive traversal of the expression tree - however, this traversal stops when an expression involving an operator not explicitly handled is encountered. For example, _1+_2-_3 will become _1-_2+_3, but _1+_1&_2 will stay as it is (there is no handler for &). let(_a = 1, _b = 2) [ _a+_b ] will also be left unchanged.

I认为这是文章的意图,但看看结尾列出的测试,我看到如果_(_ 1 * _4)[_ 2 - _3] 预计更改;使用提供的代码(此处),我

I had thought this was as intended by the article, but looking at the tests listed at the end, I see that if_(_1 * _4)[_2 - _3] is expected to change; with the code supplied (here), I find that it doesn't.

然后我如何定义一个通用的Boost Phoenix表达式树转换,该转换适用于一组显式的 all (n-ary)运算符;其他的不变?

How then can I define a generic Boost Phoenix expression tree transform which applies to all of a set of explicitly listed (n-ary) operators; leaving the others unchanged?

有些代码可能有用。我想要下面的C ++ 11代码(auto)输出 0 ,而不是 2 明确处理& 或任何其他运算符/语句。

Some code may be useful. I'd like the following C++11 code (auto) to output 0, and not 2; without explicitly handling the &, or any other operator/statement.

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

using namespace boost;
using namespace proto;
using namespace phoenix;
using namespace arg_names;

struct invrt {
  template <typename Rule> struct when : proto::_ {};
};

template <>
struct invrt::when<rule::plus>
  : proto::call<
    proto::functional::make_expr<proto::tag::minus>(
        evaluator(_left, _context), evaluator(_right, _context)
    )
  >
{};

int main(int argc, char *argv[])
{
  auto f = phoenix::eval( _1+_1&_2 , make_context(make_env(), invrt()) );
  std::cout << f(1,2) << std::endl; // Alas 2 instead of 0
  return 0;
}


推荐答案

与直接原型:

#include <iostream>
#include <boost/phoenix.hpp>
#include <boost/proto/proto.hpp>
namespace proto = boost::proto;
using namespace boost::phoenix;
using namespace arg_names;

struct invrt:
  proto::or_<
    proto::when<
      // Turn plus nodes into minus
      proto::plus<proto::_, proto::_>,
      proto::functional::make_expr<proto::tag::minus>(
        invrt(proto::_left), invrt(proto::_right)
      )
    >,
    proto::otherwise<
      // This recurses on children, transforming them with invrt
      proto::nary_expr<proto::_, proto::vararg<invrt> >
    >
  >
{};

int main(int argc, char *argv[])
{
  auto f = invrt()(_1+_1&_2);
  proto::display_expr(f);
  std::cout << f(1,2) << std::endl;
  return 0;
}



凤凰在Proto顶部堆叠了一堆东西。我不知道 pheonix :: eval 的语义或为什么你试过没有工作。也许有专业知识的凤凰会会入住。

Phoenix has layered a bunch of stuff on top of Proto. I don't know the semantics of pheonix::eval or why what you tried didn't work. Perhaps someone knowledgeable of Phoenix will chime in.

==== EDIT ====

我想出了凤凰示例的问题。它不是递归的非正的情况。您的代码应如下所示:

I figured out the problem with the Phoenix example. It's not recursing for the non-plus case. Your code should be as follows:

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

using namespace boost;
using namespace proto;
using namespace phoenix;
using namespace arg_names;

struct invrt {
  template <typename Rule>
  struct when :
    // NOTE!!! recursively transform children and reassemble
    nary_expr<_, vararg<proto::when<_, evaluator(_, _context)> > >
  {};
};

template <>
struct invrt::when<rule::plus> :
  proto::call<
    proto::functional::make_expr<proto::tag::minus>(
      evaluator(_left, _context), evaluator(_right, _context)
    )
  >
{};

int main()
{
  auto f = phoenix::eval( _1+_1&_2 , make_context(make_env(), invrt()) );
  display_expr(f);
  std::cout << f(1,2) << std::endl; // Prints 0. Huzzah!
}

无论您认为比直接Proto解决方案更简单还是更复杂决定。

Whether you consider that simpler or more complicated than the straight Proto solution is for you to decide.

这篇关于转换Boost C ++ Phoenix表达式树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆