fork(1) download
  1. #include <iostream>
  2. #include <utility>
  3. #include <typeinfo>
  4. using namespace std;
  5.  
  6. class Bar {
  7. public:
  8. Bar() { value = 5; }
  9.  
  10. Bar(Bar&& other) {
  11. value = std::move(other.value);
  12. other.value = int();
  13. }
  14.  
  15. int value;
  16. };
  17.  
  18. void foo(Bar&& bar) {
  19. Bar test1(bar); // this doesnt work !!!
  20. Bar test2(std::move(bar)); // this does work
  21. Bar test3((Bar&&)bar); //and this works too!
  22.  
  23. // yet this returns true:
  24. bool sameTypes1 = typeid(bar) == typeid(Bar&&);
  25. bool sameTypes2 = typeid(bar) == typeid(std::move(bar));
  26.  
  27. std::cout << sameTypes1 << std::endl; // true or 1
  28. std::cout << sameTypes2 << std::endl; // true or 1
  29. }
  30.  
  31. int main() {
  32. Bar bar;
  33.  
  34. foo(std::move(bar));
  35.  
  36. return 0;
  37. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void foo(Bar&&)’:
prog.cpp:19:15: error: use of deleted function ‘constexpr Bar::Bar(const Bar&)’
  Bar test1(bar);     // this doesnt work !!!
               ^
prog.cpp:6:7: note: ‘constexpr Bar::Bar(const Bar&)’ is implicitly declared as deleted because ‘Bar’ declares a move constructor or move assignment operator
 class Bar {
       ^
stdout
Standard output is empty