为什么需要转移语义
/*************************************************************************
> File Name: main.cpp
> Author: Xianghao Jia
> mail: xianghaojia@sina.com
> Created Time: Mon 09 Dec 2019 04:23:18 AM PST
************************************************************************/
#include <iostream>
using namespace std;
class Test
{
public:
Test(int a= 0)
{
d = new int(a);
cout << "构造函数" << endl;
}
Test(const Test &tmp)
{
d = new int;
*d = *(tmp.d);
cout << "拷贝构造函数" << endl;
}
~Test()
{// 析构函数
if(d != NULL)
{
delete d;
cout << "delete" << endl;
}
cout << "析构函数" << endl;
}
int *d;
};
Test GetTmp()
{
Test h;
cout << "GetTmp:Resource from " << __func__ << ":" << (void *)h.d << endl;
return h;
}
int main(int argc, char ** argv)
{
Test obj = GetTmp();
cout << "main:Resource from " << __func__ << ":" << (void *)obj.d << endl;
return 0;
}
编译器会对返回值进行优化,简称RVO,是编译器的一项优化技术,它涉及(功能是)消除为保存函数返回值而创建的临时对象
-fno-elide-constructors,此选项作用是,在g++上编译时关闭RVO。
通过上面的例子看到,临时对象的维护(创建和销毁)对性能有严重影响
右值引用是用来支持转移语义的,转移语义可以将资源(堆、系统对象等)从一个对象转移到另一个对象,这样能够减少不必要的临时对象的创建、拷贝以及销毁,能够大幅度提高C++应用程序的性能
转移语义是和拷贝语义相对的,可以类比文件的剪切与拷贝,当我们将文件从一个目录拷贝到另一个目录时,速度比剪切慢很多。
通过转移语义,临时对象中的资源能够转移至其他对象里
移动语义定义
在现有的C++机制中,我们可以定义拷贝构造函数和赋值函数。要实现转移语义,需要定义转移构造函数,还可以定义转移赋值操作符。对于右值的拷贝和赋值会调用转移构造函数和转移赋值操作符。
如果转移构造函数和转移赋值操作符没有定义,那么就遵循现有的机制,拷贝构造函数和赋值操作符会被调用
普通的函数和操作符也可以利用右值引用操作符实现转移语义。
/*************************************************************************
> File Name: main.cpp
> Author: Xianghao Jia
> mail: xianghaojia@sina.com
> Created Time: Mon 09 Dec 2019 04:23:18 AM PST
************************************************************************/
#include <iostream>
using namespace std;
class Test
{
public:
Test(int a= 0)
{
d = new int(a);
cout << "构造函数" << endl;
}
Test(const Test &tmp)
{
d = new int;
*d = *(tmp.d);
cout << "拷贝构造函数" << endl;
}
~Test()
{// 析构函数
if(d != NULL)
{
delete d;
cout << "delete" << endl;
}
cout << "析构函数" << endl;
}
Test(Test &&tmp)
{
d = tmp.d;
tmp.d = NULL;//将临时的指针成员置空
cout << "移动构造函数" << endl;
}
int *d;
};
Test GetTmp()
{
Test h;
cout << "GetTmp:Resource from " << __func__ << ":" << (void *)h.d << endl;
return h;
}
int main(int argc, char ** argv)
{
Test obj = GetTmp();
cout << "main:Resource from " << __func__ << ":" << (void *)obj.d << endl;
return 0;
}
和拷贝构造函数类似,但是有几点需要注意:
- 参数(右值)的符号必须是右值引用符号,即&&
- 参数(右值)不可以是常量,因为我们需要修改右值
- 参数(右值)的资源链接和标记必须修改,否则,右值的析构函数就会释放资源,转移到新对象的资源也就无效了
编译运行结果如下
有了右值引用和转移语义,我们在设计和实现类时,对于需要动态申请大量资源的类,应该设计转移构造函数和转移赋值函数,以提高应用程序的效率
转移赋值操作符
/*************************************************************************
> File Name: main.cpp
> Author: Xianghao Jia
> mail: xianghaojia@sina.com
> Created Time: Mon 09 Dec 2019 04:23:18 AM PST
************************************************************************/
#include <iostream>
using namespace std;
class Test
{
public:
Test(int a= 0)
{
d = new int(a);
cout << "构造函数" << endl;
}
Test(const Test &tmp)
{
d = new int;
*d = *(tmp.d);
cout << "拷贝构造函数" << endl;
}
Test &operator=(const Test &tmp)
{
if(&tmp == this)
{
return *this;
}
Test t(tmp);
swap(t.d, this->d);
}
~Test()
{// 析构函数
if(d != NULL)
{
delete d;
cout << "delete" << endl;
}
cout << "析构函数" << endl;
}
Test(Test &&tmp)
{
d = tmp.d;
tmp.d = NULL;//将临时的指针成员置空
cout << "移动构造函数" << endl;
}
Test &operator=(Test &&tmp)
{
if(&tmp == this)
{
return *this;
}
*d = *(tmp.d);
tmp.d = NULL;
cout << "转移赋值函数" << endl;
return *this;
}
int *d;
};
Test GetTmp()
{
Test h;
cout << "GetTmp:Resource from " << __func__ << ":" << (void *)h.d << endl;
return h;
}
int main(int argc, char ** argv)
{
Test obj(10);
obj = GetTmp();
cout << "main:Resource from " << __func__ << ":" << (void *)obj.d << endl;
return 0;
}
编译运行的结果如下: