fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <functional>
  4. #include <memory>
  5.  
  6. class X
  7. {
  8. std::string m_s;
  9.  
  10. public:
  11. X () { }
  12. X (std::string s) { m_s = s; }
  13. ~X () { m_s = "destroyed"; std::cout << m_s << "\n"; }
  14. X (const X&) = delete;
  15. X& operator= (const X&) = delete;
  16. X (X&& other) { m_s = other.m_s; other.m_s.clear (); }
  17. X& operator= (X&& other) { m_s = other.m_s; other.m_s.clear (); return *this; };
  18. inline void Print() const { std::cout << m_s << "\n"; }
  19. };
  20.  
  21. void foo (std::shared_ptr <X> x) { x->Print (); }
  22.  
  23. int main()
  24. {
  25. std::function <void ()> f;
  26.  
  27. {
  28. auto x = std::make_shared <X> ("Hello World!");
  29. f = [=] () { foo (x); };
  30. }
  31.  
  32. f ();
  33. }
  34.  
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
Hello World!
destroyed