C++11中std::move、std::forward、左右值引用、移动构造函数的测试问题
关于C++11新特性之std::move、std::forward、左右值引用网上资料已经很多了,我主要针对测试性能做一个测试,梳理一下这些逻辑,首先,左值比较熟悉,右值就是临时变量,意味着使用一次就不会再被使用了。针对这两种值引入了左值引用和右值引用,以及引用折叠的概念。
1.右值引用的举例测试
#includeusingnamespacestd; //创建一个测试类 classA { public: A():m_a(55) { } intm_a; }; voidfuncA(A&¶m)//右值引用参数,只接受右值 { cout< 2.左值和右值引用的举例测试,以及引出万能引用
构造一组重载函数,分别接受右值,和左值的参数,还有constA&的参数重载函数。
voidfuncA(constA¶m)//既可以接受右值引用,也可以接受左值引用,但是有一个隐式转换constA& voidfuncA(A¶m)//接受左值引用 voidfuncA(A&¶m)//接受右值引用constA¶m既可以接受右值引用,也可以接受左值引用,但是存在一个隐式转换,const使用受限制。
#includeusingnamespacestd; //创建一个测试类 classA { public: A():m_a(55)//构造函数 { cout<<"Constructor"< m_a=other.m_a; } A&operator=(constA&other)//赋值构造函数 { cout<<"=Constructor"< m_a=other.m_a; return*this; } intm_a; }; voidtest(A&&pa)//测试是否为右值 { cout<<"只接受右值"< (param));//编译不过,param可以接受右值,但是param被转换为const左值 cout< (param));//编译通过,通过forward转发 cout< (param));//编译通过,通过forward转发 cout< 对此C++11引入了万能引用的概念,使得不需要那么多的重载函数,既可以接受右值引用,也可以接受左值引用。但是函数内部,再需要调用一个左值或者右值的函数时,我们就得需要forward模版类。
#includeusingnamespacestd; //创建一个测试类 classA { public: A():m_a(newint(55))//构造函数 { cout<<"Constructor"< m_a=other.m_a; } A&operator=(constA&other)//赋值构造函数 { cout<<"=Constructor"< m_a=other.m_a; return*this; } int*m_a; }; voidtest(A&&pa)//测试是否为右值 { cout<<"只接受右值"< voidfuncA(T&¶m) { test(std::forward (param));//编译通过,通过forward完美转发 cout<<*param.m_a< 3.移动构造函数的引出
以上的所有特性,所能体现出来的是我们对于临时变量的使用,尽可能的使用中间生成的临时变量,提高性能,所谓的榨取最后的性能。移动构造函数注意的两点
1.调用移动构造函数时参数(被移动者)必须是右值。
2.调用移动构造函数后被移动者就不能再被使用。
#includeusingnamespacestd; //创建一个测试类 classA { public: A():m_a(newint(55))//构造函数 { cout<<"Constructor"< m_a=other.m_a; } A&operator=(constA&other)//赋值构造函数 { cout<<"=Constructor"< m_a=other.m_a; return*this; } A(A&&other):m_a(other.m_a)//移动构造函数,参数是一个右值, { cout<<"MoveConstructor"< voidfuncA(T&¶m) { test(std::forward (param));//编译通过,通过forward完美转发 cout<<*param.m_a< 移动构造函数一定程度上较少了临时内存的申请,减少不必要的拷贝,节省了空间和时间。以上特性在使用中还有很多需要注意的地方,如果我遇到了会及时的添加到这里,分享给大家,一起加油。
到此这篇关于C++11中std::move、std::forward、左右值引用、移动构造函数的测试问题的文章就介绍到这了,更多相关C++11中std::move、std内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。