Advertisement
sebbu

ast in C++11

Sep 4th, 2012
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3.  
  4. typedef std::unordered_map<char, double> data;
  5.  
  6. template<int D> struct val {
  7.     double operator() (const data&) const { return D; }
  8. };
  9. template<char I> struct mis {
  10.     double operator()(const data& v) const { return v.at(I); }
  11. };
  12. template<typename Expr1, typename Expr2> struct add {
  13.     double operator()(const data& v) const { return Expr1()(v)+Expr2()(v);  }
  14. };
  15. template<typename Expr1, typename Expr2> struct mul {
  16.     double operator()(const data& v) const { return Expr1()(v)*Expr2()(v); }
  17. };
  18.  
  19. int main() {
  20.     add<
  21.             mis<'a'>,
  22.             mul<
  23.                     val<2>,
  24.                     mis<'b'>
  25.             >
  26.     > expression;
  27.  
  28.     double a=1, b=2; //or whatever
  29.     std::cout << expression({ { 'a', a }, { 'b', b } }) << '\n';
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement