FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
March 01, 2003

C++ Expression Templates

(Page 10 of 18)
March 2003/C++ Expression Templates

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 —
Previous Page | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 Next Page
RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK