Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. template <class T>
  2. swap(T& a, T& b) {
  3. T tmp(a); // we now have two copies of a
  4. a = b; // we now have two copies of b (+ discarded a copy of a)
  5. b = tmp; // we now have two copies of tmp (+ discarded a copy of b)
  6. }
  7.  
  8. template <class T>
  9. swap(T& a, T& b) {
  10. T tmp(std::move(a));
  11. a = std::move(b);
  12. b = std::move(tmp);
  13. }
  14.  
  15. static_cast<T&&>(t);
  16.  
  17. int a = 3; // 3 is a rvalue, does not exist after expression is evaluated
  18. int b = a; // a is a lvalue, keeps existing after expression is evaluated
  19.  
  20. template <class T>
  21. typename remove_reference<T>::type&&
  22. std::move(T&& a)
  23. {
  24. return a;
  25. }
  26.  
  27. foo(3 * 5); // obviously, you are calling foo with a temporary (rvalue)
  28.  
  29. int a = 3 * 5;
  30. foo(a); // how to tell the compiler to treat `a` as an rvalue?
  31. foo(std::move(a)); // will call `foo(int&& a)` rather than `foo(int a)` or `foo(int& a)`
  32.  
  33. Object Object::operator+ (const Object& rhs) {
  34. Object temp (*this);
  35. // logic for adding
  36. return temp;
  37. }
  38.  
  39. Object& Object::operator+ (Object&& rhs) {
  40. // logic to modify rhs directly
  41. return rhs;
  42. }
  43.  
  44. class ResHeavy{ // ResHeavy means heavy resource
  45. public:
  46. ResHeavy(int len=10):_upInt(new int[len]),_len(len){
  47. cout<<"default ctor"<<endl;
  48. }
  49.  
  50. ResHeavy(const ResHeavy& rhs):_upInt(new int[rhs._len]),_len(rhs._len){
  51. cout<<"copy ctor"<<endl;
  52. }
  53.  
  54. ResHeavy& operator=(const ResHeavy& rhs){
  55. _upInt.reset(new int[rhs._len]);
  56. _len = rhs._len;
  57. cout<<"operator= ctor"<<endl;
  58. }
  59.  
  60. ResHeavy(ResHeavy&& rhs){
  61. _upInt = std::move(rhs._upInt);
  62. _len = rhs._len;
  63. rhs._len = 0;
  64. cout<<"move ctor"<<endl;
  65. }
  66.  
  67. // check array valid
  68. bool is_up_valid(){
  69. return _upInt != nullptr;
  70. }
  71.  
  72. private:
  73. std::unique_ptr<int[]> _upInt; // heavy array resource
  74. int _len; // length of int array
  75. };
  76.  
  77. void test_std_move2(){
  78. ResHeavy rh; // only one int[]
  79. // operator rh
  80.  
  81. // after some operator of rh, it becomes no-use
  82. // transform it to other object
  83. ResHeavy rh2 = std::move(rh); // rh becomes invalid
  84.  
  85. // show rh, rh2 it valid
  86. if(rh.is_up_valid())
  87. cout<<"rh valid"<<endl;
  88. else
  89. cout<<"rh invalid"<<endl;
  90.  
  91. if(rh2.is_up_valid())
  92. cout<<"rh2 valid"<<endl;
  93. else
  94. cout<<"rh2 invalid"<<endl;
  95.  
  96. // new ResHeavy object, created by copy ctor
  97. ResHeavy rh3(rh2); // two copy of int[]
  98.  
  99. if(rh3.is_up_valid())
  100. cout<<"rh3 valid"<<endl;
  101. else
  102. cout<<"rh3 invalid"<<endl;
  103. }
  104.  
  105. default ctor
  106. move ctor
  107. rh invalid
  108. rh2 valid
  109. copy ctor
  110. rh3 valid
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement