fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. struct Bar {
  5. int z;
  6.  
  7. Bar(int z) : z(z) {}
  8.  
  9. void add(int x, int y) { std::cout << x + y + z << std::endl; }
  10. };
  11.  
  12. template <typename Ft, Ft ff>
  13. struct Foo {
  14. template <typename... Arguments>
  15. void execute(Arguments... args) {
  16. ff(args ...);
  17. }
  18. };
  19.  
  20. template <typename T, typename Ft, Ft ff>
  21. struct FooM {
  22. template <typename... Arguments>
  23. void execute(Arguments... args) {
  24. T d(3);
  25.  
  26. std::function<void(T&, Arguments ...)> f = ff;
  27. f(d, args ...);
  28. }
  29. };
  30.  
  31. void padd(int x, int y, int z) { std::cout << x + y + z << std::endl; }
  32.  
  33. int main() {
  34. auto a = Foo<decltype(&padd), &padd>();
  35. a.execute(4, 5, 6);
  36.  
  37. auto b = FooM<Bar, decltype(&Bar::add), &Bar::add>();
  38. b.execute(4, 5);
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
15
12