fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void test1()
  5. {
  6. int x = 5;
  7. int& y = x;
  8. y = 7;
  9. cout << "x (" << &x << ") = " << x << ", ";
  10. cout << "y (" << &y << ") = " << y << endl;
  11. }
  12.  
  13. void test2() {
  14. // doesn't compile!
  15. /*
  16. int&& x = 5;
  17. int&& y = x;
  18. cout << "x (" << &x << ") = " << x << ", ";
  19. cout << "y (" << &y << ") = " << y << endl;
  20. */
  21. }
  22.  
  23. void test3()
  24. {
  25. int&& x = 5;
  26. int&& y = 8;
  27. y = x;
  28. cout << "x (" << &x << ") = " << x << ", ";
  29. cout << "y (" << &y << ") = " << y << endl;
  30. }
  31.  
  32. void test4()
  33. {
  34. int&& x = 5;
  35. int&& y = std::move(x);
  36. y = 7;
  37. cout << "x (" << &x << ") = " << x << ", ";
  38. cout << "y (" << &y << ") = " << y << endl;
  39. }
  40.  
  41. int main()
  42. {
  43. test1();
  44. test2();
  45. test3();
  46. test4();
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 5532KB
stdin
Standard input is empty
stdout
x (0x7ffd8228c124) = 7, y (0x7ffd8228c124) = 7
x (0x7ffd8228c120) = 5, y (0x7ffd8228c124) = 5
x (0x7ffd8228c124) = 7, y (0x7ffd8228c124) = 7