11.07.2015 Views

Understanding C++ Expression Templates - Angelika Langer

Understanding C++ Expression Templates - Angelika Langer

Understanding C++ Expression Templates - Angelika Langer

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

29SolutionInside the binary expression store constants of any numericaltype as Literal expressions.For this purpose define for each expression type and allnumerical types what their corresponding expression typeis:template struct exprTraits{ typedef ExprT expr_type; };template struct exprTraits{ typedef Literal expr_type; };template struct exprTraits{ typedef Literal expr_type; };...© Copyright 1995-2000 by <strong>Angelika</strong> <strong>Langer</strong>. All Rights Reserved.http://www.<strong>Angelika</strong><strong>Langer</strong>.com(57)last update: 11/7/2005 ,21:37Modify the Binary<strong>Expression</strong> ClassInside the binary expression convert expressions to theirexpression type as defined in the expression traits:template 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:exprTraits::expr_type _expr1;exprTraits::expr_type _expr2;BinOp _op;};© Copyright 1995-2000 by <strong>Angelika</strong> <strong>Langer</strong>. All Rights Reserved.http://www.<strong>Angelika</strong><strong>Langer</strong>.com(58)last update: 11/7/2005 ,21:37

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!