fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. struct PrintHello
  5. {
  6. void operator () ()
  7. {
  8. std::cout << "Hello";
  9. }
  10. };
  11.  
  12. struct PrintWorld
  13. {
  14. void operator () ()
  15. {
  16. std::cout << "World";
  17. }
  18. };
  19.  
  20. template<typename Func>
  21. class FooBar
  22. {
  23. public:
  24. FooBar()
  25. {
  26. }
  27.  
  28. FooBar(Func _func) : func(_func)
  29. {
  30. }
  31.  
  32. void print()
  33. {
  34. func();
  35. }
  36.  
  37. private:
  38. Func func;
  39. };
  40.  
  41. void freeFunction()
  42. {
  43. std::cout << "I'm free";
  44. }
  45.  
  46. int main()
  47. {
  48. FooBar<PrintHello> hello;
  49. FooBar<PrintWorld> world;
  50. FooBar<std::function<void()>> free_func(freeFunction);
  51. hello.print();
  52. std::cout << std::endl;
  53. world.print();
  54. std::cout << std::endl;
  55. free_func.print();
  56. std::cout << std::endl;
  57. return 0;
  58. }
  59.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Hello
World
I'm free