fork download
  1. #include <string>
  2. #include <functional>
  3. #include <iostream>
  4.  
  5. class Deserializer
  6. {
  7. public:
  8. int getInt() { return 10; }
  9. std::string getString() { return "test"; }
  10. };
  11.  
  12. template<typename T>
  13. T get_value(Deserializer&);
  14.  
  15. template<>
  16. int get_value(Deserializer& d)
  17. {
  18. return d.getInt();
  19. }
  20.  
  21. template<>
  22. std::string get_value(Deserializer& d)
  23. {
  24. return d.getString();
  25. }
  26.  
  27. template<typename... Args>
  28. class Dispatcher
  29. {
  30. public:
  31. template<typename Functor>
  32. Dispatcher(Deserializer& d, const Functor& cb) : myDeserializer(d), callback(cb)
  33. {
  34. }
  35. void dispatch()
  36. {
  37. callback(get_value<Args>(myDeserializer)...);
  38. }
  39. private:
  40. std::function<void(Args...)> callback;
  41. Deserializer myDeserializer;
  42. };
  43.  
  44. void cb(int a, int b, std::string c)
  45. {
  46. std::cout << a << " " << b << " " << c << std::endl;
  47. }
  48.  
  49. int main()
  50. {
  51. Deserializer d;
  52. Dispatcher<int, int, std::string> dispatcher(d, cb);
  53. dispatcher.dispatch();
  54. }
  55.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
10 10 test