fork download
  1. #include <iostream>
  2.  
  3. int func(){ return 0; }
  4. struct Foo{};
  5.  
  6. struct Bar
  7. {
  8. Bar(){ std::cout << "default ctor" << std::endl; }
  9. Bar(Bar const&){ std::cout << "copy ctor" << std::endl; }
  10. Bar(Bar&&){ std::cout << "move ctor" << std::endl; }
  11. Bar&operator=(Bar const&){ std::cout << "copy subst" << std::endl; return *this; }
  12. Bar&operator=(Bar&&){ std::cout << "move subst" << std::endl; return *this; }
  13. };
  14.  
  15. struct Baz
  16. {
  17. void f(Foo const&){ std::cout << "lvalue ref" << std::endl; }
  18. void f(Foo&&) { std::cout << "rvalue ref" << std::endl; }
  19.  
  20. void g() & { std::cout << "lvalue overload" << std::endl; }
  21. void g() && { std::cout << "rvalue overload" << std::endl; }
  22. };
  23.  
  24. int main(int,char*[])
  25. {
  26. int x = 1;
  27. Foo foo{};
  28.  
  29. x; // lvalue
  30. 1; // rvalue(prvalue)
  31.  
  32. func(); // rvalue(prvalue)
  33.  
  34. foo; // lvalue
  35. Foo{}; // rvalue(prvalue)
  36.  
  37. // lvalue reference
  38. int& lr1 = x;
  39. // int& lr2 = 1; // ERROR
  40.  
  41. // const lvalue reference
  42. int const& clr1 = x;
  43. int const& clr2 = 1;
  44.  
  45. // rvalue reference
  46. // int&& rr1 = x; // ERROR
  47. int&& rr2 = 1;
  48.  
  49. // const rvalue reference
  50. // int const&& crr1 = x; // ERROR
  51. int const&& crr2 = 1;
  52.  
  53. // move
  54. // int & lr3 = std::move(x); // ERROR
  55. int && rr3 = std::move(x); // xrvalue
  56.  
  57.  
  58. std::cout << "1: "; Bar b1{};
  59. std::cout << "2: "; Bar b2(b1);
  60. std::cout << "3: "; Bar b3(Bar{});
  61. std::cout << "4: "; Bar b4(std::move(b3));
  62.  
  63. std::cout << "5: "; b1 = b2;
  64. std::cout << "6: "; b1 = Bar{};
  65. std::cout << "7: "; b1 = std::move(b4);
  66.  
  67. Baz baz;
  68. baz.f(foo);
  69. baz.f(Foo{});
  70.  
  71. baz.g();
  72. Baz{}.g();
  73.  
  74. return 0;
  75. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:20:9: error: expected ‘;’ at end of member declaration
prog.cpp:20:14: error: expected unqualified-id before ‘{’ token
prog.cpp:20:61: error: expected ‘;’ at end of member declaration
prog.cpp:21:9: error: expected ‘;’ at end of member declaration
prog.cpp:21:7: error: ‘void Baz::g()’ cannot be overloaded
prog.cpp:20:7: error: with ‘void Baz::g()’
prog.cpp:21:14: error: expected unqualified-id before ‘{’ token
prog.cpp:21:61: error: expected ‘;’ at end of member declaration
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:29:3: warning: statement has no effect [-Wunused-value]
prog.cpp:30:3: warning: statement has no effect [-Wunused-value]
prog.cpp:34:5: warning: statement has no effect [-Wunused-value]
prog.cpp:38:7: warning: unused variable ‘lr1’ [-Wunused-variable]
prog.cpp:42:13: warning: unused variable ‘clr1’ [-Wunused-variable]
prog.cpp:43:13: warning: unused variable ‘clr2’ [-Wunused-variable]
prog.cpp:47:8: warning: unused variable ‘rr2’ [-Wunused-variable]
prog.cpp:51:14: warning: unused variable ‘crr2’ [-Wunused-variable]
prog.cpp:55:9: warning: unused variable ‘rr3’ [-Wunused-variable]
stdout
Standard output is empty