fork(11) download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. struct PassMe
  5. {
  6. PassMe()
  7. {
  8. std::cout << "default" << std::endl;
  9. }
  10. PassMe(PassMe const &)
  11. {
  12. std::cout << "copy" << std::endl;
  13. }
  14. PassMe(PassMe &&)
  15. {
  16. std::cout << "move" << std::endl;
  17. }
  18. PassMe &operator=(PassMe const &)
  19. {
  20. std::cout << "copy assign" << std::endl;
  21. return *this;
  22. }
  23. PassMe &operator=(PassMe &&)
  24. {
  25. std::cout << "move assign" << std::endl;
  26. return *this;
  27. }
  28. ~PassMe()
  29. {
  30. std::cout << "destruct" << std::endl;
  31. }
  32. void go() const
  33. {
  34. std::cout << "go" << std::endl;
  35. }
  36. };
  37.  
  38. template<typename T>
  39. auto f(T const &t)
  40. -> std::function<void()>
  41. {
  42. std::cout << "before" << std::endl;
  43. std::function<void()> l {[t]{t.go();}};
  44. std::cout << "after" << std::endl;
  45. return l;
  46. }
  47.  
  48. int main()
  49. {
  50. PassMe pm;
  51. std::cout << "pre-call" << std::endl;
  52. auto l = f(pm);
  53. std::cout << "post-call" << std::endl;
  54. l();
  55. }
  56.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
default
pre-call
before
copy
copy
destruct
after
post-call
go
destruct
destruct