fork download
  1. #include <cstring>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. // Test for copy-ellision
  7.  
  8. // A is old school class with pass-by-const-ref operator=
  9. class A
  10. {
  11. public:
  12.  
  13. static int n,d,c;
  14.  
  15. A() : ptr(0),size(0) {}
  16. A(size_t sz) : size(sz)
  17. {
  18. n++;
  19. ptr = new float[sz];
  20. }
  21.  
  22. A(A const& src)
  23. {
  24. size = src.size;
  25. n++;
  26. ptr = new float[size];
  27. c++;
  28. memcpy(ptr,src.ptr,sizeof(float)*size);
  29. }
  30.  
  31. A& operator=(A const& src)
  32. {
  33. if(this != &src)
  34. {
  35. size = src.size;
  36. d++;
  37. if(ptr) delete[] ptr;
  38. n++;
  39. ptr = new float[size];
  40. c++;
  41. memcpy(ptr,src.ptr,sizeof(float)*size);
  42. }
  43.  
  44. return *this;
  45. }
  46.  
  47. void operator()() { cout << "non-const call" << endl; }
  48.  
  49. ~A()
  50. {
  51. d++;
  52. if(ptr) delete[] ptr;
  53. ptr = 0;
  54. }
  55.  
  56. private:
  57. float* ptr;
  58. int size;
  59. };
  60.  
  61. int A::n = 0;
  62. int A::d = 0;
  63. int A::c = 0;
  64.  
  65. // B is new school class with pass-by-value operator=
  66. class B
  67. {
  68. public:
  69.  
  70. static int n,d,c;
  71.  
  72. B() : ptr(0),size(0) {}
  73. B(size_t sz) : size(sz)
  74. {
  75. n++;
  76. ptr = new float[sz];
  77. }
  78.  
  79. B(B const& src)
  80. {
  81. size = src.size;
  82. n++;
  83. ptr = new float[size];
  84. c++;
  85. memcpy(ptr,src.ptr,sizeof(float)*size);
  86. }
  87.  
  88. B& operator=(B src)
  89. {
  90. swap(src);
  91. return *this;
  92. }
  93.  
  94. void swap(B& src)
  95. {
  96. std::swap(size,src.size);
  97. std::swap(ptr,src.ptr);
  98. }
  99.  
  100. void operator()() { cout << "non-const call" << endl; }
  101.  
  102. ~B()
  103. {
  104. d++;
  105. if(ptr) delete[] ptr;
  106. ptr = 0;
  107. }
  108.  
  109. private:
  110. float* ptr;
  111. int size;
  112. };
  113.  
  114. int B::n = 0;
  115. int B::d = 0;
  116. int B::c = 0;
  117.  
  118. A sort( A const& names)
  119. {
  120. A ret = names;
  121. ret();
  122. return ret;
  123. }
  124.  
  125. B sort( B names)
  126. {
  127. names();
  128. return names;
  129. }
  130.  
  131. int main()
  132. {
  133. cout << "gcc " << __GNUC__ << "." << __GNUC_MINOR__<< endl;
  134. {
  135. A a;
  136.  
  137. a = A(5);
  138.  
  139. A c;
  140. c = sort(A(7));
  141. }
  142.  
  143. cout << "A new : " << A::n << endl;
  144. cout << "A delete : " << A::d << endl;
  145. cout << "A copy : " << A::c << endl;
  146. cout << endl;
  147.  
  148. {
  149. B a;
  150.  
  151. a = B(8);
  152.  
  153. B c;
  154. c = sort(B(9));
  155. }
  156.  
  157. cout << "B new : " << B::n << endl;
  158. cout << "B delete : " << B::d << endl;
  159. cout << "B copy : " << B::c << endl;
  160. cout << endl;
  161.  
  162. }
  163.  
Success #stdin #stdout 0.02s 2816KB
stdin
Standard input is empty
stdout
gcc 4.3
non-const call
A new : 5
A delete : 7
A copy : 3

non-const call
B new : 3
B delete : 5
B copy : 1