fork download
  1. #include <iostream>
  2.  
  3. template <typename F>
  4. struct Foo {
  5. Foo(F&& f) : f(std::forward<F>(f)) {}
  6.  
  7. template<typename... Arguments>
  8. void execute(Arguments... args) {
  9. f(args ...);
  10. }
  11.  
  12. protected:
  13. F f;
  14. };
  15.  
  16. template <typename F>
  17. Foo<F> make_foo(F&& f = F()) { return {f}; }
  18.  
  19. void padd(int a, int b) { std::cout << a + b << std::endl; }
  20. void psub(int a, int b) { std::cout << a - b << std::endl; }
  21.  
  22. int main() {
  23. auto f = make_foo(padd);
  24. f.execute(5, 6);
  25.  
  26. make_foo(psub).execute(5, 6);
  27.  
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
11
-1