gpt4 book ai didi

c++ - 转换 Boost C++ Phoenix 表达式树

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:55 24 4
gpt4 key购买 nike

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

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

我原以为这是文章的意图,但查看最后列出的测试,我看到 if_(_1 * _4)[_2 - _3] 预计会改变;使用提供的代码 ( here ),我发现它没有。

然后我如何定义一个通用的 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;
}

最佳答案

这就是使用直接 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 的人会插话。

==== 编辑 ====

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

关于c++ - 转换 Boost C++ Phoenix 表达式树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15077637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com