fork download
  1. #include <iostream>
  2.  
  3. enum COMMANDS {
  4. CMD_ZERO,
  5. CMD_ONE,
  6. CMD_TWO,
  7. };
  8.  
  9. template <COMMANDS cmd> struct command
  10. {
  11. template <typename ... Args>
  12. int operator() (Args&&...) const { return -1; }
  13. };
  14.  
  15. template <> struct command<CMD_ZERO>
  16. {
  17. int operator()(double a, double b, double c) const
  18. {
  19. std::cout << "cmd0 " << a << ", " << b << ", " << c << std::endl;
  20. return 0;
  21. }
  22. };
  23.  
  24. template <> struct command<CMD_ONE>
  25. {
  26. int operator()(int a, int b, int c) const
  27. {
  28. std::cout << "cmd1 " << a << ", " << b << ", " << c << std::endl;
  29. return 1;
  30. }
  31. };
  32.  
  33. template <COMMANDS cmd, typename... Args> int DispatchCommand(Args&&... args)
  34. {
  35. return command<cmd>()(std::forward<Args>(args)...);
  36. }
  37.  
  38. int main()
  39. {
  40. DispatchCommand<CMD_ZERO>(1, 3.141, 4);
  41. DispatchCommand<CMD_ONE>(5, 6, 7);
  42. DispatchCommand<CMD_TWO>(5, 6, 7, 8, 9);
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
cmd0  1, 3.141, 4
cmd1  5, 6, 7