diff --git a/examples/et/2_generic_arity.cpp b/examples/et/2_generic_arity.cpp new file mode 100644 index 0000000..2b44aee --- /dev/null +++ b/examples/et/2_generic_arity.cpp @@ -0,0 +1,61 @@ +#include +#include + +/* unchanged from et/1_first_draft.cpp */ +struct Add { + template + static auto eval(T lhs, T rhs) { + return lhs + rhs; + } +}; + +struct Mul { + template + static auto eval(T lhs, T rhs) { + return lhs * rhs; + } +}; + +template +class Value { + T _value; + +public: + Value(T value): _value(value) {} + + auto eval() const { return _value; } +}; +/* end of unchanged */ + +template +class Expr { + std::tuple _operands; + +public: + Expr(Operands... operands): _operands{operands...} {} + + auto eval() const { + return eval(std::make_index_sequence{}); + } + +private: + template + auto eval(std::index_sequence) const { + return Op::eval(std::get(_operands).eval()...); + } +}; + +/* unchanged from et/1_first_draft.cpp */ +int main() { + using SubExpr = Expr, Value>; + using MyExpr = Expr, SubExpr>; + + int volatile v0 = 3, v1 = 5, v2 = 7; + int result; + + MyExpr expr{{v0}, {{v1}, {v2}}}; + result = expr.eval(); + + std::printf("%d\n", result); +} +/* end of unchanged */