mp/examples/et/1_first_draft.cpp

49 lines
765 B
C++
Raw Normal View History

2019-07-02 15:21:34 +00:00
#include <cstdio>
struct Add {
template<typename T>
static auto eval(T lhs, T rhs) {
return lhs + rhs;
}
};
struct Mul {
template<typename T>
static auto eval(T lhs, T rhs) {
return lhs * rhs;
}
};
template<typename T>
class Value {
T _value;
public:
Value(T value): _value(value) {}
auto eval() const { return _value; }
};
template<typename Op, typename LHS, typename RHS>
struct BinExpr {
LHS lhs;
RHS rhs;
auto eval() const {
return Op::eval(lhs.eval(), rhs.eval());
}
};
int main() {
using SubExpr = BinExpr<Mul, Value<int>, Value<int>>;
using MyExpr = BinExpr<Add, Value<int>, SubExpr>;
int volatile v0 = 3, v1 = 5, v2 = 7;
int result;
MyExpr expr{{v0}, {{v1}, {v2}}};
result = expr.eval();
std::printf("%d\n", result);
}