fork download
  1. #include <iostream>
  2.  
  3. // prints something;
  4. // the template doesn't add anything
  5. template <typename T>
  6. struct Printer
  7. {
  8. void print()
  9. {
  10. std::cout << "Printer::print" << std::endl;
  11. }
  12. };
  13.  
  14. // this is an actual template
  15. // calls the method indicated by the second template argument
  16. // belonging to the class indicated by the first template argument
  17. template < typename U, void(U::*func)()>
  18. struct Caller
  19. {
  20. void call(U obj)
  21. {
  22. (obj.*func)();
  23. }
  24. };
  25.  
  26. // just a wrapper
  27. template<typename V>
  28. struct Wrapper
  29. {
  30. void call_caller_on_printer()
  31. {
  32. Printer<int> a_printer;
  33. Caller<Printer<int>, &Printer<int>::print> caller;
  34. caller.call(a_printer);
  35. }
  36. };
  37.  
  38. int main()
  39. {
  40. Wrapper<int> the_wrapper;
  41. the_wrapper.call_caller_on_printer();
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0.02s 2724KB
stdin
Standard input is empty
stdout
Printer::print