使用模板的 C++ 惰性求值

C++ lazy evaluation with templates

我写了一个c++程序来实现惰性二进制操作。

该程序应该按如下方式工作:创建一个带运算的二叉树,每次我们在二叉运算中使用一棵树和其他东西时,我们把两个东西都作为左和右 children 的新的操作。当我调用 operator T 时,它需要每个 child 的另一个 operator T 递归,以防它也是一个运算符,或者将它传递给与模板参数关联的函数(F).

代码

#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)(non-operator) class,但它似乎(参见 is_recursive)知道 compile-time 中有哪些类型。

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

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

this answer