C++移动构造函数,移动赋值和编译选项-fno-elide-constructors

示例代码:

 
  1. #include <iostream>

  2. using namespace std;

  3. class Array2D {

  4. private:

  5. int m,n; // m rows, n cols

  6. int *array;

  7. public:

  8. Array2D(int _m, int _n):m(_m), n(_n) {

  9. cout << "ctor" << endl;

  10. array = new int[m*n];

  11. }

  12. Array2D(const Array2D &a) {

  13. cout << "copy ctor" << endl;

  14. int j,k;

  15. m = a.m;

  16. n = a.n;

  17. array = new int[m*n];

  18. for(int i = 0; i < m*n; ++i) {

  19. k = i % a.n;

  20. j = (i - k) /n;

  21. array[i] = a(j, k);

  22. }

  23. }

  24. Array2D(Array2D &&a) noexcept {

  25. cout << "move ctor" << endl;

  26. m = a.m;

  27. n = a.n;

  28. array = a.array;

  29. a.array = nullptr;

  30. }

  31. ~Array2D() {

  32. delete[] array;

  33. cout << "dtor" << endl;

  34. }

  35. int& operator()(int x, int y) {

  36. return array[x*n+y];

  37. }

  38. const int& operator()(int x, int y) const {

  39. return array[x*n+y];

  40. }

  41. Array2D& operator=(const Array2D &a) {

  42. cout << "call operator=" << endl;

  43. if (this->array != a.array) {

  44. int j,k;

  45. this->m = a.m;

  46. this->n = a.n;

  47. delete[] this->array;

  48. this->array = new int[m*n];

  49. for(int i = 0; i < m*n; ++i) {

  50. k = i % a.n;

  51. j = (i - k) /n;

  52. this->array[i] = a(j, k); //calling: const int& operator()(int x, int y) const

  53. }

  54. }

  55. return *this;

  56. }

  57. Array2D& operator=(Array2D &&a) {

  58. cout << "call move operator=" << endl;

  59. m = a.m;

  60. n = a.n;

  61. array = a.array;

  62. a.array = nullptr;

  63. return *this;

  64. }

  65. };

  66. Array2D getArray2D() {

  67. cout << "in getArray2D()" << endl;

  68. Array2D a2d(1000,1000);

  69. a2d(100,100) = 100;

  70. a2d(200,200) = 200;

  71. a2d(900,900) = 900;

  72. return a2d;

  73. }

(1)main中的测试代码:

 
  1. Array2D big1 = getArray2D();

  2. cout << big1(100,100) << " " << big1(200,200) << " " << big1(900,900) << endl;

C++98

C++11 & C++14

C++17 & C++20

With

-fno-elide-constructors

in getArray2D()

ctor

copy ctor

dtor

copy ctor

dtor

100 200 900

dtor

in getArray2D()

ctor

move ctor

dtor

move ctor

dtor

100 200 900

dtor

in getArray2D()

ctor

move ctor

dtor

100 200 900

dtor

Without

-fno-elide-constructors

in getArray2D()

ctor

100 200 900

dtor

显示详细信息

(2)main中的测试代码:

 
  1. Array2D big3(1,1);

  2. big3 = getArray2D();

  3. cout << big3(100,100) << " " << big3(200,200) << " " << big3(900,900) << endl;

C++98

C++11 & C++14 & C++17 & C++20

With

-fno-elide-constructors

ctor

in getArray2D()

ctor

copy ctor

dtor

call operator=

dtor

100 200 900

dtor

ctor

in getArray2D()

ctor

move ctor

dtor

call move operator=

dtor

100 200 900

dtor

Without

-fno-elide-constructors

tor

in getArray2D()

ctor

call operator=

dtor

100 200 900

dtor

ctor

in getArray2D()

ctor

call move operator=

dtor

100 200 900

dtor

显示详细信息

说明:使用C++98标准编译时,需要注释掉移动构造函数和移动赋值函数

-fno-elide-constructors

The C++ standard allows an implementation to omit creating a temporary that is only used to initialize another object of the same type. Specifying this option disables that optimization, and forces G++ to call the copy constructor in all cases.

 默认情况下,不带-fno-elide-constructors选项,编译器会进行良好的优化。

C++移动构造函数,移动赋值和编译选项-fno-elide-constructors-优快云博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值