fork download
  1.  
  2. #include <iostream>
  3.  
  4. struct fooA {
  5. int foo1(int x, int y) { return x + y; }
  6. int foo2(int x, double y, void *) { return x + y; }
  7. };
  8. struct fooB {
  9. void foo1(int x, int y) { std::cout << "fooB(" << x << ", " << y << ")\n"; }
  10. void foo2(int x, double y, void *z) { std::cout << "fooB(" << x << ", " << y << ", " << z << ")\n"; }
  11. };
  12. struct fooC {
  13. void foo1(int x, int y) { std::cout << "fooC(" << x << ", " << y << ")\n"; }
  14. void foo2(int x, double y, void *z) { std::cout << "fooC(" << x << ", " << y << ", " << z << ")\n"; }
  15. };
  16.  
  17. #define Return(ret) decltype ret { return ret; }
  18.  
  19. struct foo1Caller
  20. {
  21. template <typename T, typename... Args>
  22. auto operator () (T& t, Args... args) -> Return((t.foo1(args...)))
  23. };
  24.  
  25. struct foo2Caller
  26. {
  27. template <typename T, typename... Args>
  28. auto operator () (T& t, Args... args) -> Return((t.foo2(args...)))
  29. };
  30.  
  31.  
  32. class foo
  33. {
  34. public:
  35. void foo1(int x, int y)
  36. {
  37. dispatch(foo1Caller(), x, y);
  38. }
  39. void foo2(int x, double y, void *z)
  40. {
  41. dispatch(foo2Caller(), x, y, z);
  42. }
  43.  
  44. private:
  45. template <typename T, typename... Args>
  46. void dispatch(T caller, Args... args)
  47. {
  48. switch (caller(m_Member, args...)) {
  49. case 1: caller(m_Member1, args...); return;
  50. case 2: caller(m_Member2, args...); return;
  51. }
  52. }
  53.  
  54. fooA m_Member;
  55. fooB m_Member1;
  56. fooC m_Member2;
  57. };
  58.  
  59. int main()
  60. {
  61. foo dispatcher;
  62. dispatcher.foo1(1, 0);
  63. dispatcher.foo2(1, 1, 0);
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
fooB(1, 0)
fooC(1, 1, 0)