Guest User

Untitled

a guest
Jun 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cassert>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. class Double;
  7. Double&& move(Double&& a) { return a; }
  8.  
  9.  
  10. static inline int IDENTIFY(const void* ptr) { return (((int)ptr) >> 2) & 0xFF; }
  11.  
  12. class Double {
  13. public:
  14. double value;
  15. Double(const double _value) : value(_value) {
  16. printf("Constructor, [%x][?] := [?][%lg].\n", IDENTIFY(this), value);
  17. }
  18. Double(const Double& other) : value(other.value) {
  19. printf("Copy constructor, [%x][?] := [%x][%lg]\n", IDENTIFY(this), IDENTIFY(&other), value);
  20. }
  21. Double(Double&& other) : value(other.value) {
  22. printf("Move constructor, [%x][?] <- [%x][%lg]\n", IDENTIFY(this), IDENTIFY(&other), value);
  23. other.value = nan("move");
  24. }
  25. ~Double() {
  26. value = nan("dstr");
  27. printf("Destructor, delete [%x][%lg]\n", IDENTIFY(this), value);
  28. }
  29. Double& operator= (const Double& other) {
  30. printf("Copy operator, [%x][%lg] := [%x][%lg]\n", IDENTIFY(this), value, IDENTIFY(&other), other.value);
  31. value = other.value;
  32. return *this;
  33. }
  34. Double& operator= (Double&& other) {
  35. printf("Move operator, [%x][%lg] <- [%x][%lg]\n", IDENTIFY(this), value, IDENTIFY(&other), other.value);
  36. value = other.value;
  37. return *this;
  38. }
  39. Double& operator+= (const Double& other) {
  40. printf("Increament, [%x][%lg] += [%x][%lg]\n", IDENTIFY(this), value, IDENTIFY(&other), other.value);
  41. value += other.value;
  42. return *this;
  43. }
  44.  
  45. bool operator== (const double v) { return v == value; }
  46. };
  47.  
  48. Double operator+ (const Double& self, const Double& other) {
  49. printf("Addition (copy), return [%x][%lg] + [%x][%lg]\n", IDENTIFY(&self), self.value, IDENTIFY(&other), other.value);
  50. Double copy = self;
  51. return (Double&&)(copy += other);
  52. }
  53.  
  54. Double operator+ (Double&& self, const Double& other) {
  55. printf("Addition (temp), return [%x][%lg] + [%x][%lg]\n", IDENTIFY(&self), self.value, IDENTIFY(&other), other.value);
  56. return (Double&&)(self += other);
  57. }
  58.  
  59. int main () {
  60. Double a = 1.;
  61. Double b = 10.;
  62. Double c = a;
  63. Double d = b;
  64. Double e = c;
  65. Double z = 0;
  66. assert(a == 1);
  67. assert(b == 10);
  68. assert(c == 1);
  69. assert(d == 10);
  70. assert(e == 1);
  71. assert(z == 0);
  72.  
  73. printf("\n\nf = a + z + a + z + c + z + z + z;\n");
  74. Double f = a + z + a + z + c + z + z + z;
  75. assert(f == 3);
  76. assert(a == 1);
  77. assert(c == 1);
  78. assert(z == 0);
  79.  
  80. printf("\n\nf += a;\n");
  81. f += a;
  82. assert(f == 4);
  83. assert(a == 1);
  84.  
  85. printf("\n\ne = b;\n");
  86. e = b;
  87. assert(e == 10);
  88. assert(b == 10);
  89.  
  90. printf("\n\na = c + a + f;\n");
  91. a = c + a + z + z + f;
  92. assert(c == 1);
  93. assert(f == 4);
  94. assert(a == 6);
  95. assert(z == 0);
  96.  
  97. return 0;
  98. }
  99.  
  100. /* Result:
  101.  
  102. Constructor, [e2][?] := [?][1].
  103. Constructor, [e0][?] := [?][10].
  104. Copy constructor, [de][?] := [e2][1]
  105. Copy constructor, [dc][?] := [e0][10]
  106. Copy constructor, [da][?] := [de][1]
  107. Constructor, [d8][?] := [?][0].
  108.  
  109.  
  110. f = a + z + a + z + c + z + z + z;
  111. Addition (copy), return [e2][1] + [d8][0]
  112. Copy constructor, [ca][?] := [e2][1]
  113. Increament, [ca][1] += [d8][0]
  114. Move constructor, [ee][?] <- [ca][1]
  115. Destructor, delete [ca][nan]
  116. Addition (temp), return [ee][1] + [e2][1]
  117. Increament, [ee][1] += [e2][1]
  118. Move constructor, [ec][?] <- [ee][2]
  119. Addition (temp), return [ec][2] + [d8][0]
  120. Increament, [ec][2] += [d8][0]
  121. Move constructor, [ea][?] <- [ec][2]
  122. Addition (temp), return [ea][2] + [de][1]
  123. Increament, [ea][2] += [de][1]
  124. Move constructor, [e8][?] <- [ea][3]
  125. Addition (temp), return [e8][3] + [d8][0]
  126. Increament, [e8][3] += [d8][0]
  127. Move constructor, [e6][?] <- [e8][3]
  128. Addition (temp), return [e6][3] + [d8][0]
  129. Increament, [e6][3] += [d8][0]
  130. Move constructor, [e4][?] <- [e6][3]
  131. Addition (temp), return [e4][3] + [d8][0]
  132. Increament, [e4][3] += [d8][0]
  133. Move constructor, [d6][?] <- [e4][3]
  134. Destructor, delete [e4][nan]
  135. Destructor, delete [e6][nan]
  136. Destructor, delete [e8][nan]
  137. Destructor, delete [ea][nan]
  138. Destructor, delete [ec][nan]
  139. Destructor, delete [ee][nan]
  140.  
  141.  
  142. f += a;
  143. Increament, [d6][3] += [e2][1]
  144.  
  145.  
  146. e = b;
  147. Copy operator, [da][1] := [e0][10]
  148.  
  149.  
  150. a = c + a + f;
  151. Addition (copy), return [de][1] + [e2][1]
  152. Copy constructor, [ca][?] := [de][1]
  153. Increament, [ca][1] += [e2][1]
  154. Move constructor, [f6][?] <- [ca][2]
  155. Destructor, delete [ca][nan]
  156. Addition (temp), return [f6][2] + [d8][0]
  157. Increament, [f6][2] += [d8][0]
  158. Move constructor, [f4][?] <- [f6][2]
  159. Addition (temp), return [f4][2] + [d8][0]
  160. Increament, [f4][2] += [d8][0]
  161. Move constructor, [f2][?] <- [f4][2]
  162. Addition (temp), return [f2][2] + [d6][4]
  163. Increament, [f2][2] += [d6][4]
  164. Move constructor, [f0][?] <- [f2][6]
  165. Move operator, [e2][1] <- [f0][6]
  166. Destructor, delete [f0][nan]
  167. Destructor, delete [f2][nan]
  168. Destructor, delete [f4][nan]
  169. Destructor, delete [f6][nan]
  170. Destructor, delete [d6][nan]
  171. Destructor, delete [d8][nan]
  172. Destructor, delete [da][nan]
  173. Destructor, delete [dc][nan]
  174. Destructor, delete [de][nan]
  175. Destructor, delete [e0][nan]
  176. Destructor, delete [e2][nan]
  177.  
  178. */
Add Comment
Please, Sign In to add comment