使用模板进行C ++延迟评估

时间:2016-12-21 15:58:20

标签: c++ templates binary-tree c++14 lazy-evaluation

我编写了一个c ++程序来实现延迟二进制操作。

该程序应该按如下方式工作:使用操作创建一个二叉树,每次我们在二进制操作中使用一个树时,我们将这两个东西作为新操作的左右子项。当我调用operator T时,如果每个子节点也是一个运算符,它还需要递归另一个operator T,否则将它传递给与模板参数(F)相关联的函数。 / p>

代码

#include <iostream>
#include <cassert>
#include <vector>

static const auto &add = [](auto a, auto b) {return a + b;};
static const auto &sub = [](auto a, auto b) {return a - b;};
static const auto &mul = [](auto a, auto b) {return a * b;};
static const auto &frac = [](auto a, auto b) {return a / b;};

template <class A, class B, class F> class BinaryOP;

template <class A, class B> using Addition = BinaryOP <A, B, decltype(add)>;
template <class A, class B> using Subtraction = BinaryOP <A, B, decltype(sub)>;
template <class A, class B> using Multiplication = BinaryOP <A, B, decltype(mul)>;
template <class A, class B> using Division = BinaryOP <A, B, decltype(frac)>;

static const auto make_binop = [](auto a, auto b, auto &F) { return BinaryOP<decltype(a), decltype(b), decltype(F)>(a, b, F); };
static const auto make_add = [](auto a, auto b) { return make_binop(a, b, add); };
static const auto make_sub = [](auto a, auto b) { return make_binop(a, b, sub); };
static const auto make_mul = [](auto a, auto b) { return make_binop(a, b, mul); };
static const auto make_div = [](auto a, auto b) { return make_binop(a, b, frac); };

template <class T>
struct is_recursable {
private:
  typedef char yes;
  typedef short no;
  template <typename C> static yes test(decltype(C::RECURSE_ME)*);
  template <typename C> static no test(...);
public:
  enum { value = sizeof(test<T>(0)) == sizeof(yes)  };
};

template <class A, class B, class F>
class BinaryOP {
protected:
  typedef BinaryOP <A, B, F> THIS_T;
  const A lhs;
  const B rhs;
  F func;
public:
  static const bool RECURSE_ME;

  constexpr BinaryOP(const A &lhs, const B &rhs, F &func):
    lhs(lhs), rhs(rhs), func(func)
  {}

  virtual ~BinaryOP()
  {}

  template <class T>
  constexpr operator T() const {
    auto a = is_recursable<A>::value ? T(lhs) : lhs;
    auto b = is_recursable<B>::value ? T(rhs) : rhs;
    return func(a, b);
  }

  template <class R>
  constexpr Addition <THIS_T, R> operator+(const R &b) {
    return make_add(*this, b);
  }

  template <class R>
  constexpr Subtraction <THIS_T, R> operator-(const R &b) {
    return make_sub(*this, b);
  }

  template <class R>
  constexpr Multiplication <THIS_T, R> operator*(const R &b) {
    return make_mul(*this, b);
  }

  template <class R>
  constexpr Division <THIS_T, R> operator/(const R &b) {
    return make_div(*this, b);
  }

  friend std::ostream &operator<<(std::ostream &os, const THIS_T &tree) {
    os << "{" << tree.lhs << ":" << is_recursable<A>::value << ", " << tree.rhs << ":" << is_recursable<B>::value << "}";
    return os;
  }

  void printrec() const {
    printf("1: {");
    if(is_recursable<A>::value == 1)
      /* printf("1 %s", typeid(B).name()); */
      lhs.printrec();
    else
      printf("0");
    printf(", ");
    if(is_recursable<B>::value == 1)
      /* printf("1 %s", typeid(B).name()); */
      rhs.printrec();
    else
      printf("0");
    printf("}");
  }
};

class Matrix {
  typedef std::vector <float> vec_t;
  typedef std::vector <std::vector <float> > mat_t;
  mat_t mat;
public:
  Matrix(mat_t mat):
    mat(mat)
  {}
  Matrix(size_t h, size_t w):
    mat(mat_t(h, vec_t(w)))
  {
    for(size_t i = 0; i < h; ++i)
      for(size_t j = 0; j < w; ++j)
        mat[i][j] = (i == j);
  }
  size_t height() const { return mat.size(); }
  size_t width() const { return (height() == 0) ? 0 : mat.front().size(); }
  Matrix operator*(const Matrix &other) {
    Matrix res(height(), other.width());
    assert(width() == other.height());
    for(size_t i = 0; i < height(); ++i) {
      for(size_t j = 0; j < other.width(); ++j) {
        float sum = 0.;
        for(size_t k = 0; k < width(); ++k) {
          sum += mat[i][k] * other.mat[k][j];
        }
        res.mat[i][j] = sum;
      }
    }
    return res;
  }
  Matrix operator*(const float &scalar) {
    Matrix res(height(), width());
    for(size_t i = 0; i < height(); ++i) for(size_t j = 0; j < width(); ++j) res.mat[i][j] = mat[i][j] * scalar;
    return res;
  }
  friend std::ostream &operator<<(std::ostream &os, const Matrix &m) {
    for(size_t i = 0; i < m.height(); ++i) {
      for(size_t j = 0; j < m.width(); ++j) {
        os << m.mat[i][j] << " ";
      }
      os << std::endl;
    }
    return os;
  }
};

int main() {
  auto tree = make_add(2, 3) + make_sub(5, 6) * make_add(3, 0) / 3; // = 4
  std::cout << tree << std::endl;
  std::cout << int(tree) << std::endl;
  Matrix m(10, 10), n(10, 10);
  auto mattree = make_mul(m, n) + 6;
  std::cout << mattree << std::endl;
  std::cout << "operator is recursable: " << is_recursable<Addition<float, Matrix> >::value << std::endl;
  std::cout << "matrix is recursable: " << is_recursable<Matrix>::value << std::endl;
  std::cout << "float is recursable: " << is_recursable<float>::value << std::endl;
  std::cout << "mattree is recursable: " << is_recursable<decltype(mattree)>::value << std::endl;
  mattree.printrec();
  /* std::cout << (mattree.operator Matrix()) << std::endl; */
}

编译

c++ -flto -O2 -std=c++1y binary_operator.cpp

binary_operator.cpp:93:10: error: member reference base type 'const int' is not a structure or union
      rhs.printrec();
      ~~~^~~~~~~~~
binary_operator.cpp:158:11: note: in instantiation of member function 'BinaryOP<BinaryOP<Matrix, Matrix, const <lambda at binary_operator.cpp:7:26> &>, int, const <lambda at binary_operator.cpp:5:26> &>::printrec' requested here
  mattree.printrec();
          ^
binary_operator.cpp:87:11: error: no member named 'printrec' in 'Matrix'
      lhs.printrec();
      ~~~ ^
binary_operator.cpp:87:11: note: in instantiation of member function 'BinaryOP<Matrix, Matrix, const <lambda at binary_operator.cpp:7:26> &>::printrec' requested here
      lhs.printrec();
          ^
binary_operator.cpp:158:11: note: in instantiation of member function 'BinaryOP<BinaryOP<Matrix, Matrix, const <lambda at binary_operator.cpp:7:26> &>, int, const <lambda at binary_operator.cpp:5:26> &>::printrec' requested here
  mattree.printrec();
          ^
binary_operator.cpp:93:11: error: no member named 'printrec' in 'Matrix'
      rhs.printrec();
      ~~~ ^
3 errors generated.
make: *** [binary_operator] Error 1

GCC

binary_operator.cpp: In instantiation of 'void BinaryOP<A, B, F>::printrec() const [with A = BinaryOP<Matrix, Matrix, const<lambda(auto:5, auto:6)>&>; B = int; F = const<lambda(auto:1, auto:2)>&]':
binary_operator.cpp:158:20:   required from here
binary_operator.cpp:93:11: error: request for member 'printrec' in '((const BinaryOP<BinaryOP<Matrix, Matrix, const<lambda(auto:5, auto:6)>&>, int, const<lambda(auto:1, auto:2)>&>*)this)->BinaryOP<BinaryOP<Matrix, Matrix, const<lambda(auto:5, auto:6)>&>, int, const<lambda(auto:1, auto:2)>&>::rhs', which is of non-class type 'const int'
       rhs.printrec();
       ~~~~^~~~~~~~
binary_operator.cpp: In instantiation of 'void BinaryOP<A, B, F>::printrec() const [with A = Matrix; B = Matrix; F = const<lambda(auto:5, auto:6)>&]':
binary_operator.cpp:87:7:   required from 'void BinaryOP<A, B, F>::printrec() const [with A = BinaryOP<Matrix, Matrix, const<lambda(auto:5, auto:6)>&>; B = int; F = const<lambda(auto:1, auto:2)>&]'
binary_operator.cpp:158:20:   required from here
binary_operator.cpp:87:11: error: 'const class Matrix' has no member named 'printrec'
       lhs.printrec();
       ~~~~^~~~~~~~
binary_operator.cpp:93:11: error: 'const class Matrix' has no member named 'printrec'
       rhs.printrec();
       ~~~~^~~~~~~~

shell returned 1

但如果我通过改变评论来改变printrec(参见代码),我会得到:

{{2:0, 3:0}:1, {{{5:0, 6:0}:1, {3:0, 0:0}:1}:1, 3:0}:1}
4
{{1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
:0, 1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
:0}:1, 6:0}
operator is recursable: 1
matrix is recursable: 0
float is recursable: 0
mattree is recursable: 1
1: {1 i, 0}

显然,当我尝试递归地评估(或打印)树时,它无法使用错误进行编译,因为它无法在叶(非运算符)类中找到相同的方法(例如printrec),但它似乎(参见is_recursive)它知道编译时存在哪些类型。

1 个答案:

答案 0 :(得分:1)

if(is_recursable<A>::value == 1)
  /* printf("1 %s", typeid(B).name()); */
  lhs.printrec();
else
  printf("0");

constexpr if存在之前,您无法执行此操作。原因是编译器必须假设两个分支都是可能的,即使一个或另一个是不可达的 - 是的,它似乎应该能够分辨,但语言不允许它。您需要使用标签分派或其他编译时构造。

请参阅this answer