fork download
  1. #include <iostream>
  2.  
  3. struct X {
  4. char const* name;
  5.  
  6. X(char const* name) : name(name) { std::cout << name << " constructed @" << this << std::endl; }
  7. X(X&& other) {
  8. name = other.name;
  9. other.name = "moved_obj";
  10. std::cout << name << " moved @" << this << " <- @" << &other << std::endl;
  11. }
  12. ~X() { std::cout << name << " destroyed @" << this << std::endl; }
  13. };
  14.  
  15. int main() {
  16. X a("a");
  17. int x;
  18. std::cin >> x;
  19. X&& rv = x == 10 ? std::move(a) : X("b");
  20. std::cout << "Using " << rv.name << "\n";
  21. }
Success #stdin #stdout 0s 2836KB
stdin
10
stdout
a constructed @0xbfbf310c
a moved @0xbfbf3104 <- @0xbfbf310c
Using a
a destroyed @0xbfbf3104
moved_obj destroyed @0xbfbf310c