fork download
  1. #include <tuple>
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <functional>
  6.  
  7. std::map<char, std::function<int(int, int)> > ops =
  8. {
  9. {'+', [](int f, int s) { return f + s; } }
  10. };
  11.  
  12. int calc(int value)
  13. {
  14. return value;
  15. }
  16.  
  17. template<typename Left, typename Right>
  18. int calc(const std::tuple<char, Left, Right>& tuple)
  19. {
  20. char symbol = std::get<0>(tuple);
  21. Left l_child = std::get<1>(tuple);
  22. Right r_child = std::get<2>(tuple);
  23. int l = calc(l_child);
  24. int r = calc(r_child);
  25. return ops[symbol](l, r);
  26. }
  27.  
  28. int main()
  29. {
  30. auto left(std::make_tuple('+', 1, 2));
  31. auto right(std::make_tuple('+', 5, 6));
  32. auto expr(std::make_tuple('+', left, right));
  33. std::cout << calc(expr) << std::endl;
  34. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
14