fork(1) download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. #define print_func() cout << __PRETTY_FUNCTION__ << endl
  6.  
  7. struct A {
  8. A() {print_func();}
  9. A(A &) {print_func();}
  10. A(A &&) {print_func();}
  11. A(const A &) {print_func();}
  12. A(const A &&) {print_func();}
  13. ~A() {print_func();}
  14. };
  15.  
  16. void bad_free(A && a) {
  17. cout << "inside bad_free, before move" << endl;
  18. A && clever = move( a );
  19. cout << "inside bad_free, after move" << endl;
  20. // 'clever' should be the last reference to a?
  21. }
  22.  
  23. int main() {
  24. {
  25. cout << "test1 start" << endl;
  26. bad_free( A() );
  27. cout << "test1 end" << endl;
  28. }
  29. {
  30. cout << "test2 start" << endl;
  31. A x;
  32. cout << "test2 before call" << endl;
  33. bad_free( move(x) );
  34. cout << "test2 end" << endl;
  35. }
  36. return 0;
  37. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
test1 start
A::A()
inside bad_free, before move
inside bad_free, after move
A::~A()
test1 end
test2 start
A::A()
test2 before call
inside bad_free, before move
inside bad_free, after move
test2 end
A::~A()