日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++11中的右值引用及move语义编程

發(fā)布時(shí)間:2023/12/10 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++11中的右值引用及move语义编程 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

C++0x中加入了右值引用,和move函數(shù)。右值引用出現(xiàn)之前我們只能用const引用來(lái)關(guān)聯(lián)臨時(shí)對(duì)象(右值)(造孽的VS可以用非const引用關(guān)聯(lián)臨時(shí)對(duì)象,請(qǐng)忽略VS),所以我們不能修臨時(shí)對(duì)象的內(nèi)容,右值引用的出現(xiàn)就讓我們可以取得臨時(shí)對(duì)象的控制權(quán),終于可以修改臨時(shí)對(duì)象了!而且書(shū)上說(shuō)配合move函數(shù),可以大大提高現(xiàn)有C++的效率。那么是怎樣提高它的效率的呢?看段代碼先!

#include <iostream> #include <utility> #include <vector> #include <string> int main() {std::string str = "Hello";std::vector<std::string> v;// uses the push_back(const T&) overload, which means // we'll incur the cost of copying str v.push_back(str);std::cout << "After copy, str is \"" << str << "\"\n";// uses the rvalue reference push_back(T&&) overload, // which means no strings will copied; instead, the contents// of str will be moved into the vector. This is less// expensive, but also means str might now be empty. v.push_back(std::move(str));std::cout << "After move, str is \"" << str << "\"\n";std::cout << "The contents of the vector are \"" << v[0]<< "\", \"" << v[1] << "\"\n"; }

Output:

After copy, str is "Hello" After move, str is "" The contents of the vector are "Hello", "Hello"

?

看完大概明白一點(diǎn)兒了,加上move之后,str對(duì)象里面的內(nèi)容被"移動(dòng)"到新的對(duì)象中并插入到數(shù)組之中了,同時(shí)str被清空了。這樣一來(lái)省去了對(duì)象拷貝的過(guò)程。所以說(shuō)在str對(duì)象不再使用的情況下,這種做法的效率更高一些!但問(wèn)題是str的內(nèi)容在什么地方被移走的呢?move函數(shù)到底是干啥的?扣一下stl源碼吧,下面是move模板的源碼:

// TEMPLATE FUNCTION movetemplate<class _Ty> inlinetypename tr1::_Remove_reference<_Ty>::_Type&&move(_Ty&& _Arg){ // forward _Arg as movablereturn ((typename tr1::_Remove_reference<_Ty>::_Type&&)_Arg);}

?

好吧,看過(guò)了這段,可能有人又迷惑了,不是說(shuō)有名左指變量不能綁定到右值引用上面么?為什么move函數(shù)的參數(shù)是右值引用卻可以接受左值變量作為參數(shù)?難道STL錯(cuò)了么?事實(shí)上,C++0x在引入右值引用的時(shí)候?qū)瘮?shù)模板自動(dòng)推導(dǎo)也加入了新的規(guī)則,簡(jiǎn)單的說(shuō),像例子中的這種情況,模板參數(shù)是_Ty而函數(shù)的參數(shù)是_Ty&&(右值引用),同時(shí)_Arg是string的左值對(duì)象的情況下,會(huì)觸發(fā)一個(gè)特殊規(guī)則,_Ty會(huì)推導(dǎo)成string&,也就是說(shuō)此事推導(dǎo)出來(lái)的函數(shù)與move<string&>一致。那么move(_Ty&& _Arg) 得到的應(yīng)該是move(string& && _Arg)這個(gè)時(shí)候根據(jù)引用折疊原則,會(huì)變成這個(gè)樣子move(string& _Arg)。詳細(xì)的描述參見(jiàn)白云飄飄翻譯的vc技術(shù)文檔(http://www.cppblog.com/kesalin/archive/2009/06/05/86851.html)。函數(shù)的返回值嘛,就好說(shuō)了,就是返回所持有類型的右值引用了。所以,move函數(shù)的作用很簡(jiǎn)單,不管你給什么參數(shù),都返回對(duì)應(yīng)類型的右值引用!那么,上面例子中str的不是在move函數(shù)中被移走的。綜上,我們猜測(cè)str內(nèi)容肯定是在構(gòu)造新對(duì)象的過(guò)程中被新對(duì)象偷走的,也就是在string的參數(shù)為右值引用的構(gòu)造函數(shù)中被偷走的!翻看string的源碼(來(lái)自VS實(shí)現(xiàn)的STL),果然如此啊!如下:

basic_string(_Myt&& _Right): _Mybase(_STD forward<_Alloc>(_Right._Alval)){ // construct by moving _Right _Tidy();assign(_STD forward<_Myt>(_Right));} _Myt& assign(_Myt&& _Right){ // assign by moving _Rightif (this == &_Right);else if (get_allocator() != _Right.get_allocator()&& this->_BUF_SIZE <= _Right._Myres)*this = _Right;else{ // not same, clear this and steal from _Right_Tidy(true);if (_Right._Myres < this->_BUF_SIZE)_Traits::move(this->_Bx._Buf, _Right._Bx._Buf,_Right._Mysize + 1);else{ // copy pointerthis->_Bx._Ptr = _Right._Bx._Ptr;_Right._Bx._Ptr = 0;}this->_Mysize = _Right._Mysize;this->_Myres = _Right._Myres;_Right._Tidy();}return (*this);}

?

所以,我們知道了,C++0x在STL模板庫(kù)中加入了參數(shù)為右值引用的構(gòu)造函數(shù),用于把參數(shù)所關(guān)聯(lián)對(duì)象中的數(shù)據(jù)移動(dòng)到新對(duì)象當(dāng)中,避免了深度拷貝,增加了效率。再詳細(xì)翻看源碼,可以發(fā)現(xiàn)除了構(gòu)造函數(shù),operator=也重載了一個(gè)參數(shù)為右值引用的函數(shù),用途和構(gòu)造函數(shù)類似。所以我們自定義中的類也應(yīng)該增加參數(shù)為右值引用的構(gòu)造函數(shù)和重載賦值運(yùn)算符!原因是啥,看例子!

未定義參數(shù)為右值引用的構(gòu)造函數(shù):

#include <iostream> #include <utility> #include <vector> #include <string>using namespace std;class MyPoint{ public:MyPoint():comment(""), x(0), y(0){}MyPoint(const MyPoint& p):comment(p.comment),x(p.x),y(p.y) {} //MyPoint(MyPoint&& p)// :comment(move(p.comment)), x(p.x), y(p.y)//{// p.x = 0;// p.y = 0;//}string toString(){char buf[100];sprintf(buf, "%s: %d %d", comment.c_str(), x, y);return buf;}string comment;int x;int y;};int main() {MyPoint p;p.comment = "First point";p.x = 9;p.y = 7;vector<MyPoint> v;v.push_back(p);cout << "After copy, str is \"" << p.toString() << "\"\n";v.push_back(move(p));cout << "After move, str is \"" << p.toString() << "\"\n";cout << "The contents of the vector are \"" << v[0].toString()<< "\", \"" << v[1].toString() << "\"\n";cin.get(); }

結(jié)果:

After copy, str is "First point: 9 7" After move, str is "First point: 9 7" The contents of the vector are "First point: 9 7", "First point: 9 7"

?

定義了參數(shù)為右值引用的構(gòu)造函數(shù)之后:

After copy, str is "First point: 9 7" After move, str is ": 0 0" The contents of the vector are "First point: 9 7", "First point: 9 7"

?

綜上所述,C++0x中的move語(yǔ)義編程,不僅僅是在應(yīng)用的時(shí)候使用參數(shù)中加上move,對(duì)于自定義類需要增加參數(shù)為右值引用的構(gòu)造函數(shù)和賦值運(yùn)算符,這種構(gòu)造函數(shù)我們稱為move構(gòu)造函數(shù)!公司里面的c++標(biāo)準(zhǔn)已經(jīng)更新,要求在定義copy構(gòu)造函數(shù)的同時(shí)定義move構(gòu)造函數(shù),雖然現(xiàn)在這種編程方法沒(méi)有流行,但是我相信以后這將成為另外一個(gè)媲美引用的優(yōu)化運(yùn)行速度的編程方法,我們拭目以待!

總結(jié)

以上是生活随笔為你收集整理的C++11中的右值引用及move语义编程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。