fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <string>
  4.  
  5. class Node
  6. {
  7. std::function<std::string()> execute;
  8. public:
  9. Node(std::function<std::string()> _execute)
  10. : execute(std::move(_execute))
  11. {
  12. }
  13. void output() const
  14. {
  15. std::cout << execute() << '\n';
  16. }
  17. };
  18.  
  19. std::string f1() { return "f1"; }
  20. struct T { std::string f2() { return "f2"; } };
  21. struct F { std::string operator ()() const { return "f3"; } };
  22.  
  23. int main()
  24. {
  25. T obj;
  26.  
  27. Node n1 { f1 };
  28. Node n2 { std::bind(&T::f2, &obj) };
  29. Node n3 { F() };
  30. Node n4 { []() { return std::string("f4"); } };
  31.  
  32. n1.output();
  33. n2.output();
  34. n3.output();
  35. n4.output();
  36. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
f1
f2
f3
f4