[英]C++ move semantics and code duplication as a result of method overloading
在開始之前,我在其他 stackoverflow 帖子中沒有找到可以完全解釋我的問題的任何內容,因此我決定創建自己的。 抱歉,如果它已經在其他地方得到了回答(如果它確實存在,請把我指向現有的帖子)。
假設我們在一個類中有以下兩個方法:
int do_stuff(Thing& thing){ /* Do stuff... */} // L-value version
int do_stuff(Thing&& thing){ /* Do stuff... */} // R-value version
從我讀過的內容來看,現代 C++ 幾乎已經放棄了這種邏輯,並且建議只通過值傳遞Thing
,讓編譯器來做它的魔法。 我的問題是,如果我想有兩個單獨的方法來顯式處理 L 值/R 值並避免代碼重復,以下哪一個是最好的(性能明智和最佳實踐)?
int do_stuff(Thing& thing){ return do_stuff(std::move(thing)); } // The L-value version uses the R-value one
或者
int do_stuff(Thing&& thing){ return do_stuff(thing); } // The R-value version uses the L-value one since text is an L-value inside the scope of do_stuff(Thing&&)
編輯:這個問題的目的是讓我理解這個簡單的移動語義案例,而不是創建一個有效的 C++ API。
編輯 #2:問題的print
和std::string
部分用作示例。 他們可以是任何東西。
編輯 #3:重命名示例代碼。 這些方法確實修改了 Thing 對象。
如果print
沒有改變任何東西而只打印字符串,最好采用const std::string &
作為const std::string &
能夠綁定到左值和右值。
int print(const std::string& text) {}
按值傳遞參數並不意味着它不能是rvalue
引用。 &&
只是意味着參數必須是一個rvalue
引用。 沒有&&
並不意味着參數不能是rvalue
引用。
當參數僅由功能使用,並與我的意思是,如果它不修改,聲明你的功能最好的辦法是:
int do_stuff(const Thing& thing);
這樣,讀者很清楚thing
不會被修改。 對於大多數其他情況,您應該簡單地將函數聲明為:
int do_stuff(Thing thing);
按值傳遞參數,而不是按引用或rvalue
引用。
以前寫這樣的代碼很常見:
int do_stuff(Thing& thing)
{
/* change thing so that the caller can use the changed thing */
return success; // where success is an int
}
但是,如今,通常更喜歡返回修改后的東西:
Thing do_stuff(Thing thing) { /* return modified thing */ }
在上面的例子中:
int do_stuff(Thing thing);
調用者決定thing
是否應該是副本:
do_stuff(my_thing); // copy - I need the original my_thing
do_stuff(std::move(thing)); // no copy - I don't need the original my_thing
請注意,此do_stuff
聲明涵蓋了您的兩個版本:
int do_stuff(Thing&);
int do_stuff(Thing&&);
也就是說,你幾乎從不需要這樣的函數:
int do_stuff(Thing&&);
除非對於不能像stream
對象一樣復制的對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.