fork download
  1. #include <iostream>
  2.  
  3. //Returns r-value
  4. int Add(int x, int y) {
  5. return x + y;
  6. }
  7. //Return l-value
  8. int & Transform(int &x) {
  9. x *= x;
  10. return x;
  11. }
  12.  
  13. void Print(int &x) {
  14. std::cout << "Print(int&) L value ref" << std::endl;
  15. }
  16. void Print(const int &x) {
  17. std::cout << "Print(const int&) const l value ref" << std::endl;
  18.  
  19. }
  20. void Print(int &&x) {
  21. std::cout << "Print(int &&) r value ref" << std::endl;
  22. }
  23. int main() {
  24. //x is lvalue
  25. int x = 10;
  26.  
  27. //ref is l-value reference
  28. int &ref = x ;
  29. //Transform returns an l-value
  30. int &ref2 = Transform(x) ;
  31. //Binds to function that accepts l-value reference
  32. Print(x);
  33.  
  34. Print(ref);
  35. Print(ref2);
  36.  
  37. //rv is r-value reference
  38. int &&rv = 8 ;
  39.  
  40. //Add returns a temporary (r-value)
  41. int &&rv2 = Add(3,5) ;
  42. //Binds to function that accepts a temporary, i.e. r-value reference
  43. Print(3);
  44. Print(std::move(rv));
  45. Print(std::move(rv2));
  46. return 0;
  47. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Print(int&) L value ref
Print(int&) L value ref
Print(int&) L value ref
Print(int &&) r value ref
Print(int &&) r value ref
Print(int &&) r value ref