fork download
  1. //rvalue classes and move function
  2. template <class T>
  3. struct r_ref {
  4. T& ref;
  5. r_ref(T& rhs) :ref(rhs) {}
  6. r_ref(const r_ref& rhs) :ref(rhs.ref) {}
  7. };
  8. template <class T>
  9. struct r_val : r_ref<T> {
  10. mutable T val;
  11. r_val(T& rhs) :r_ref<T>(val), val(rhs) {}
  12. r_val(const r_ref<T>& rhs) :r_ref<T>(val), val(rhs) {}
  13. };
  14. template <class T>
  15. r_ref<T> move(T& rhs) {return r_ref<T>(rhs);}
  16.  
  17.  
  18.  
  19.  
  20.  
  21. //demo classes
  22. #include <iostream>
  23. #include <string>
  24. class demo {
  25. public:
  26. std::string data;
  27. demo() : data("LONG STRING SO NO CHEATS") {std::cout<<"default construct ";}
  28. demo(const demo& rhs) : data(rhs.data.begin(),rhs.data.end()) {std::cout<<"copy construct ";}
  29. demo(const r_ref<demo>& rhs) : data("MOVED FROM NO CHEATS") {data.swap(rhs.ref.data); std::cout<<"move construct ";}
  30. demo& operator=(const demo& rhs) {data.assign(rhs.data.begin(),rhs.data.end()); std::cout<<"copy assignment "; return *this;}
  31. demo& operator=(const r_ref<demo>& rhs) {data.swap(rhs.ref.data); std::cout<<"move assignment "; return *this;}
  32. ~demo() {std::cout<<"destruct ";}
  33. void prove() const {std::cout<<(void*)data.c_str()<<' '<<data.c_str();}
  34. };
  35.  
  36. r_val<demo> function(demo rhs) {
  37. std::cout << "\nwant move assignment: ";
  38. demo obj(move(rhs));
  39. std::cout << "\nwant VAR1: ";
  40. obj.prove();
  41. std::cout << "\nwant move construct, destruct, destruct, move construct, destruct:\n\t";
  42. return move(obj);
  43. }
  44.  
  45.  
  46.  
  47. //test suite
  48. int main() {
  49. std::cout << "\nwant default construct: "; demo a;
  50. std::cout << "\nwant VAR1: "; a.prove();
  51. std::cout << "\nwant move construct: "; demo b = function(move(a));
  52. std::cout << "\nwant VAR2: "; a.prove();
  53. std::cout << "\nwant VAR1: "; b.prove();
  54. std::cout << "\nwant copy assignment: "; a = b;
  55. std::cout << "\nwant VAR3: "; a.prove();
  56. std::cout << "\nwant VAR1: "; b.prove();
  57. std::cout << "\nwant move assign: "; a = move(b);
  58. std::cout << "\nwant VAR1: "; a.prove();
  59. std::cout << "\nwant VAR3: "; b.prove();
  60. std::cout << "\nwant 2x destruct: ";
  61. }
  62.  
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
want default construct: default construct 
want VAR1: 0x9c18014 LONG STRING SO NO CHEATS
want move construct: move construct 
want move assignment: move construct 
want VAR1: 0x9c18014 LONG STRING SO NO CHEATS
want move construct, destruct, destruct, move construct, destruct:
	move construct destruct move construct destruct destruct 
want VAR2: 0x9c18044 MOVED FROM NO CHEATS
want VAR1: 0x9c18014 LONG STRING SO NO CHEATS
want copy assignment: copy assignment 
want VAR3: 0x9c180bc LONG STRING SO NO CHEATS
want VAR1: 0x9c18014 LONG STRING SO NO CHEATS
want move assign: move assignment 
want VAR1: 0x9c18014 LONG STRING SO NO CHEATS
want VAR3: 0x9c180bc LONG STRING SO NO CHEATS
want 2x destruct: destruct destruct