49 lines
765 B
C++
49 lines
765 B
C++
#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);
|
|
}
|