我可以给出不同的行为来提升::p roto::标签类型吗?

Can I give different behaviours to boost::proto::tag types?

本文关键字:roto 标签 类型 我可以      更新时间:2023-10-16

我正在尝试使用boost proto来懒惰地评估表达式,我想做的是能够为+,-,函数等标签赋予不同的行为。

function(
    terminal(8functionILi2EE)
  , plus(
        multiplies(
            terminal(6tensorILi0EE)
          , terminal(6tensorILi1EE)
        )
      , multiplies(
            terminal(6tensorILi2EE)
          , terminal(6tensorILi3EE)
        )
    )
)

对于像上面这样的树,我希望能够指定每个树节点的行为方式。

例如。

struct context : proto::callable_context< context const >
{
    // Values to replace the tensors
    std::vector<double> args;
    // Define the result type of the zero.
    // (This makes the zero_context "callable".)
    typedef double result_type;
    // Handle the tensors:
    template<int I>
    double operator()(proto::tag::terminal, tensor<I>) const
    {
        std::cout << this->args[I] << std::endl;
        return this->args[I];
    }
    template<int I>
    void operator()(proto::tag::plus) const
    {
        std::cout << " + " << std::endl;
    }
};

当我这样做时

double result = (_tensorA + _tensorB)(10, 20);

我希望我的输出是

10
+
20

但这只是

10
20

任何帮助将不胜感激! :)

template<int I>
void operator()(proto::tag::plus) const
{
    std::cout << " + " << std::endl;
}

模板参数I是不可推导的,因此重载将永远不适用。删除模板参数:

void operator()(proto::tag::plus) const
{
    std::cout << " + " << std::endl;
}

但是,您真正想要的是拦截二进制运算符。井。请注意,它是二进制的。所以它有两个参数:

template<size_t I, size_t J>
void operator()(proto::tag::plus, proto::literal<tensor<I>>&, proto::literal<tensor<J>>&) const {
    std::cout << " + " << std::endl;
}

住在科里鲁

但是,这会阻止表达式树的进一步计算。不是你想要的,对吧。因此,让我们做一个简单的重新实现:

template<size_t I, size_t J>
double operator()(proto::tag::plus, proto::literal<tensor<I>>& a, proto::literal<tensor<J>>& b) const {
    auto va = (*this)(proto::tag::terminal{}, a.get());
    std::cout << " + " << std::endl;
    auto vb = (*this)(proto::tag::terminal{}, b.get());
    return va + vb;
}

住在科里鲁

通用,请

但是,有些东西告诉我你想要泛型表达式。所以t1 + (t2 + t3)也应该有效,但(t2 + t3)不是字面意思......

让我们通过委派来简化:

template<typename A, typename B>
double operator()(proto::tag::plus, A& a, A& b) const {
    auto va = proto::eval(a, *this);
    std::cout << " + " << std::endl;
    auto vb = proto::eval(b, *this);
    return va + vb;
}

完整样本

住在科里鲁

#include <boost/proto/proto.hpp>
#include <vector>
namespace proto = boost::proto;
template <size_t N> struct tensor { };
template <size_t N, size_t M> tensor<N+M> operator+(tensor<N>, tensor<M>) { return {}; }
struct context : proto::callable_context< context const >
{
    using base_type = proto::callable_context<context const>;
    // Values to replace the tensors
    std::vector<double> args { 0, 111, 222, 333 };
    // Define the result type of the zero.
    // (This makes the zero_context "callable".)
    typedef double result_type;
    // Handle the tensors:
    template<size_t I>
    double operator()(proto::tag::terminal, tensor<I>) const
    {
        std::cout << this->args[I] << std::endl;
        return this->args[I];
    }
    template<typename A, typename B>
    double operator()(proto::tag::plus, A& a, B& b) const {
        auto va = proto::eval(a, *this);
        std::cout << " + " << std::endl;
        auto vb = proto::eval(b, *this);
        return va + vb;
    }
};
int main() {
    proto::literal<tensor<1> > t1;
    proto::literal<tensor<2> > t2;
    proto::literal<tensor<3> > t3;
    auto r = proto::eval(t1 + (t2 + t3), context());
    std::cout << "eval(t1 + (t2 + t3)) = " << r << "n";
}

指纹

111
 + 
222
 + 
333
eval(t1 + (t2 + t3)) = 666