5

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

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

我原以为这与文章的意图一致,但看看最后列出的测试,我发现这种if_(_1 * _4)[_2 - _3]情况预计会发生变化;使用提供的代码(此处),我发现它没有。

那么如何定义一个通用的 Boost Phoenix 表达式树变换,它适用于所有一组显式列出的(n 元)运算符;保持其他不变?

一些代码可能有用。我希望输出以下 C++11 代码(自动)0,而不是2没有明确处理&, 或任何其他运算符/语句。

#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;
}
4

1 回答 1

2

这就是你使用直接 Proto 的方式:

#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;
}

Phoenix 在 Proto 之上分层了一堆东西。我不知道pheonix::eval您尝试的方法的语义或为什么不起作用。也许了解凤凰的人会插话。

==== 编辑 ====

我发现了 Phoenix 示例的问题。对于非加号情况,它不会递归。您的代码应如下所示:

#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 解决方案更简单或更复杂由您决定。

于 2013-02-26T18:30:26.447 回答