我在生命週期結束之前將一些對象傳遞給構造函數。爲什麼移動比通過const&?更加昂貴?
int main(){
//I wanted to avoid short string optimization in this example.
const std::string a(25,"a");
const std::string b(25,"b");
const std::string c(25,"c");
Foo f{a,b,c};
}
我爲Foo的構造函數考慮了兩個選項。
const&
(調用字符串的拷貝構造函數):
Foo(const std::string & a,
const std::string & b,
const std::string & c)
:a(a)
,b(b)
,c(c)
{}
std::move
(調用字符串的移動構造函數):
Foo(std::string a,
std::string b,
std::string c)
:a(std::move(a))
,b(std::move(b))
,c(std::move(c))
{}
隨着-01
上gcc 7
,我得到了以下結果:
+-------------+-----------------------+
| constructor | assembly instructions |
+-------------+-----------------------+
| const& | 192 |
| move | 264 |
+-------------+-----------------------+
爲什麼是const&
減指令?
我認爲一個舉動會比通過複製構造函數創建一個新字符串便宜。
什麼是作爲構造函數參數傳遞變量的經驗法則
當這些參數的生命週期結束?
從'main'中的'a,b,c'中刪除'const'和參數;並在函數調用中使用'move' –