fork download
  1. #include <functional>
  2. #include <iostream>
  3.  
  4. class Foo
  5. {
  6. public:
  7. void bar() { std::cout << "Member function: Foo::bar()\n"; }
  8. };
  9.  
  10. void bar()
  11. {
  12. std::cout << "Free function: bar()\n";
  13. }
  14.  
  15. class Functor
  16. {
  17. public:
  18. void operator()() { std::cout << "Functor object\n"; }
  19. };
  20.  
  21. auto lambda = []() { std::cout << "Lambda expression\n"; };
  22.  
  23. void doSomething(std::function<void ()> fn)
  24. {
  25. fn();
  26. }
  27.  
  28. int main()
  29. {
  30. doSomething(bar);
  31. doSomething(Functor());
  32. doSomething(lambda);
  33.  
  34. Foo foo;
  35. doSomething(std::bind(&Foo::bar, &foo));
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
Free function: bar()
Functor object
Lambda expression
Member function: Foo::bar()