fork(1) download
  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4.  
  5. template <class T>
  6. inline void out(const T& t) { cout << t << endl; }
  7. inline void out(const char* str) { cout << str << endl; }
  8. template <class T>
  9. inline void out(const char* str, const T& t)
  10. { cout << str << ' ' << t << endl; }
  11.  
  12. class Obj
  13. {
  14. private:
  15. static int count;
  16. int num;
  17. public:
  18. Obj() : num(count++) { out("new", num); }
  19. ~Obj() { out("delete", num); }
  20. };
  21. int Obj::count = 0;
  22.  
  23. unique_ptr<Obj> foo() {
  24. out("in foo");
  25. unique_ptr<Obj> tmp(new Obj);
  26. out("out foo");
  27. return tmp;
  28. }
  29.  
  30. int main() {
  31. out("start");
  32. unique_ptr<Obj> o(new Obj);
  33. unique_ptr<Obj> e, p;
  34. out("call foo");
  35. o = foo();
  36. e = move(o);
  37. out("back to main");
  38. o = foo();
  39. p.swap(o);
  40. out(o.get());
  41. out(e.get());
  42. out(p.get());
  43. out("finish");
  44. return 0;
  45. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
start
new 0
call foo
in foo
new 1
out foo
delete 0
back to main
in foo
new 2
out foo
0
0x9712018
0x9712008
finish
delete 2
delete 1