何時應(yīng)該在函數(shù)返回值上使用std :: move?在這種情況下struct Foo {};Foo meh() { return std::move(Foo());}我很確定移動是不必要的,因為新創(chuàng)建的Foo將是一個xvalue。但在這種情況下呢?struct Foo {};Foo meh() { Foo foo; //do something, but knowing that foo can safely be disposed of //but does the compiler necessarily know it? //we may have references/pointers to foo. how could the compiler know? return std::move(foo); //so here the move is needed, right?}我認(rèn)為需要采取行動嗎?
3 回答

至尊寶的傳說
TA貢獻(xiàn)1789條經(jīng)驗 獲得超10個贊
在這兩種情況下都不需要這樣做。在第二種情況下,std::move
因為你按值返回一個局部變量是多余的,編譯器會理解,因為你不再使用那個局部變量,所以它可以被移動而不是被復(fù)制。
- 3 回答
- 0 關(guān)注
- 2078 瀏覽
添加回答
舉報
0/150
提交
取消