首页 > 其他分享 >std:move() 作用 和 移动语义后 右值行为,unique_ptr的"移动"操作问题

std:move() 作用 和 移动语义后 右值行为,unique_ptr的"移动"操作问题

时间:2022-09-22 17:57:36浏览次数:51  
标签:std 移动 move unique ptr 构造函数

unique_ptr 不能进行赋值操作,但是可以有返回unique_ptr的函数,由此产生的问题:

 

结论1:std:move() 只是将一个实参强行转换为右值引用。

我们知道对象初始化时有 构造函数拷贝构造函数移动构造函数;其中移动构造函数能够防止拷贝过程,减小性能开销;

1.拷贝构造函数通常使用赋值运算可以触发,如T a=b;

2.而移动构造函数需要使用右值引用来赋值,因此通常需要搭配std:move()使用
  T a=std:move(b);或者T a = func(); 其中func() 一个返回T类型的函数;

举个例子:

 1 #include <iostream>
 2 #include <memory>
 3 using namespace std;
 4 
 5 class A {
 6 public:
 7     A() {
 8         cout << "构造函数" << endl;
 9     }
10     A(const A& a) {
11         cout << "拷贝构造" << endl;
12     }
13     A(A&& a) {
14         cout << "移动构造" << endl;
15     }
16     virtual ~A() {}
17 };
18 
19 A func(A a){
20     cout<<"func body"<<endl;
21     return a;  
22 }
23 
24 int main() {
25     A a;
26     cout<<endl;
27     A b=a;
28     cout<<endl;
29     A c=move(a);
30     cout<<endl;   
31     A d=func(a); 
32     return 0;
33 }

 

运行结果:

构造函数

拷贝构造

移动构造

拷贝构造
func body
移动构造

 


结论2:那么在移动语义后右值将会如何呢,答案是调用析构函数销毁。

  如 T a =std:move(b); 语句后 b将被销毁。

举个例子:

 1 #include <iostream>
 2 #include <memory>
 3 using namespace std;
 4 int main() {
 5     unique_ptr<int> ptr1(new int(10));
 6     unique_ptr<int> ptr2=move(ptr1);
 7     if(ptr1==nullptr) 
 8         cout<<"ptr1==null "<<endl;
 9     //cout<<*ptr1<<endl;        //引发段错误,已析构
10     cout <<&ptr1 << endl << &ptr2 << endl;
11     return 0;
12 }

运行结果:

1 ptr1==null 
2 0x7fffd55dc548
3 0x7fffd55dc550

 

标签:std,移动,move,unique,ptr,构造函数
From: https://www.cnblogs.com/wined/p/16720265.html

相关文章