Listing 3: Source code for template-based solution
class Literal { public: Literal(const double v) : _val(v) {} double eval() const { return _val; } private: const double _val; }; class Variable { public: Variable(double& v) : _val(v) {} double eval() const { return _val; } private: double& _val; }; template <class ExprT1,class ExprT2, class BinOp> class BinaryExpr { public: BinaryExpr(ExprT1 e1, ExprT2 e2,BinOp op=BinOp()) : _expr1(e1),_expr2(e2),_op(op) {} double eval() const { return _op(_expr1.eval(),_expr2.eval()); } private: ExprT1 _expr1; ExprT2 _expr2; BinOp _op; }; ... End of Listing