fork download
  1. #include <string>
  2. #include <iostream>
  3. #include <functional>
  4.  
  5. // remember a struct is just a class whose members are, by default public!
  6. struct hello_message
  7. {
  8. // function operator of type std::string(std::string const &) const
  9. std::string operator () (std::string const & name) const
  10. {
  11. return "hello, " + name;
  12. }
  13. };
  14.  
  15. // this expects a functor to execute
  16. void execute_functor(
  17. std::function<std::string (std::string)> const & functor
  18. )
  19. {
  20. auto && msg = functor("evilrix");
  21. std::cout << msg << std::endl;
  22. }
  23.  
  24. int main()
  25. {
  26. // create the function object
  27. auto && hellomsg = hello_message();
  28.  
  29. // pass to the executor
  30. execute_functor(hellomsg);
  31. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
hello, evilrix