C++为保持源代码优雅而不失高效率,提供了不少措施,其中右值引用、移动构造函数是一个非常重要的机制。C++ 不愧编程语言之王,java、C# 等后来者,实际上从她身上吸取了不少精华。
看下面代码:
1. 一个例子
#include <iostream>
using namespace std;
class A {
public:
int x;
A(int x) : x(x)
{
cout << "Constructor" << endl;
}
A(A& a) : x(a.x)
{
cout << "Copy Constructor" << endl;
}
A& operator=(A& a)
{
x = a.x;
cout << "Copy Assignment operator" << endl;
return *this;
}
A(A&& a) : x(a.x)
{
cout << "Move Constructor" << endl;
}
A& operator=(A&& a)
{
x = a.x;
cout << "Move Assignment operator" << endl;
return *this;
}
};
A GetA()
{
return A(1);
}
A&& MoveA()
{
return A(1);
}
int main()
{
cout << "-------------------------1-------------------------" << endl;
A a(1); //Constructor
cout << "-------------------------2-------------------------" << endl;
A b = a; //Copy Constructor
cout << "-------------------------3-------------------------" << endl;
A c(a); //Copy Constructor
cout << "-------------------------4-------------------------" << endl;
b = a; //Copy Assignment operator
cout << "-------------------------5-------------------------" << endl;
A d = A(1); //Constructor
cout << "-------------------------6-------------------------" << endl;
A e = std::move(a); //Move Constructor
cout << "-------------------------7-------------------------" << endl;
A f = GetA(); //Constructor
cout << "-------------------------8-------------------------" << endl;
A&& g = MoveA(); //Constructor
cout << "-------------------------9-------------------------" << endl;
d = A(1); //Constructor
//Move Assignment operator
system("pause");
}
在我电脑上运行了一下,结果如下:
-------------------------1-------------------------
Constructor
-------------------------2-------------------------
Copy Constructor
-------------------------3-------------------------
Copy Constructor
-------------------------4-------------------------
Copy Assignment operator
-------------------------5-------------------------
Constructor
-------------------------6-------------------------
Move Constructor
-------------------------7-------------------------
Constructor
-------------------------8-------------------------
Constructor
-------------------------9-------------------------
Constructor
Move Assignment operator
请按任意键继续. . .
2. 结果分析
我想说说这里面的特色:
cout << "-------------------------2-------------------------" << endl;
A b = a;
cout << "-------------------------3-------------------------" << endl;
A c(a);
cout << "-------------------------4-------------------------" << endl;
b = a;
这几行代码调用 copy 复制构造函数、复制函数,需要把 a 中的数据复制到 b 中。当数据类型 A 数据量很大时,这行代码耗费内存和时间,应该尽量避免这些用法。
其实变量 a 的值在很多情况下,复制完成后就可以丢弃了,因此,可以采用移动操作来完成相关功能。
cout << "-------------------------6-------------------------" << endl;
A e = std::move(a);
cout << "-------------------------7-------------------------" << endl;
A f = GetA();
cout << "-------------------------8-------------------------" << endl;
A&& g = MoveA();
cout << "-------------------------9-------------------------" << endl;
d = A(1);
实际上,上述结果中如果 7 没有优化编译的话,应该是下面的结果:
-------------------------7-------------------------
Constructor
Move Constructor
Move Constructor
从这里也能看出,C++编译器也是很强大的,可以自动简化不必要的操作。当然,移动构造函数的机制和右值引用的机制是编译器能够有效进行优化的基础。
3. 右值引用、移动构造函数、移动操作
8 中引入了右值引用的概念,A&& 是右值引用,表明 MoveA() 的值在赋值语句完成后就删除掉了,因此,g 承接了这个被抛弃的值的空间。
另外 stl 库中提供了 std::move 操作,用来实现数据移动操作。