fork download
  1. #include <iostream>
  2.  
  3. template<typename Functor, unsigned = 0>
  4. struct Wrapper
  5. {
  6. static void DoCallback(Functor f)
  7. {
  8. f();
  9. }
  10. };
  11. template<typename Functor>
  12. struct Wrapper<Functor, 1>
  13. {
  14. static void DoCallback(Functor f)
  15. {
  16. f(1, 2);
  17. }
  18. };
  19. template<typename Functor>
  20. void DoCallback(Functor f)
  21. {
  22. Wrapper<Functor>::DoCallback(f);
  23. }
  24.  
  25. struct f1
  26. {
  27. void operator()(){std::cout << "f1" << std::endl;}
  28. };
  29. struct f2
  30. {
  31. void operator()(int x, int y){std::cout << "f2 with " << x << ", " << y << std::endl;}
  32. };
  33.  
  34.  
  35. int main()
  36. {
  37. f1 a;
  38. f2 b;
  39. DoCallback(a);
  40. DoCallback(b);
  41. }
  42.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘static void Wrapper<Functor, <anonymous> >::DoCallback(Functor) [with Functor = f2; unsigned int <anonymous> = 0u]’:
prog.cpp:22:5:   required from ‘void DoCallback(Functor) [with Functor = f2]’
prog.cpp:40:17:   required from here
prog.cpp:8:9: error: no match for call to ‘(f2) ()’
prog.cpp:29:8: note: candidate is:
prog.cpp:31:10: note: void f2::operator()(int, int)
prog.cpp:31:10: note:   candidate expects 2 arguments, 0 provided
stdout
Standard output is empty