In Boost Phoenix's article “Transforming an Expression Tree” here , a set of custom class specializations invert_actionsare used to invert binary arithmetic expressions. For example, a+bit becomes a-b; a*bbecomes a/b; and vice versa for both.
This implies a recursive traversal of the expression tree — however, this traversal stops when an expression is encountered in which the statement is not explicitly processed. For example, it _1+_2-_3will become _1-_2+_3, but _1+_1&_2will remain as it is (the handler for &) is absent. let(_a = 1, _b = 2) [ _a+_b ]will also remain unchanged.
I thought this was done in accordance with this article, but looking at the tests listed at the end, I see what if_(_1 * _4)[_2 - _3]is expected; with the code provided ( here ), I find that it is not.
How then can I define a general Boost Phoenix expression tree that applies to the whole set of explicitly listed (n-ary) operators; leaving the rest unchanged?
Some code may be helpful. I would like the following C ++ 11 (auto) code to output 0, not 2; without explicit processing &or any other operator / operator.
#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;
return 0;
}
source
share