vault backup: 2022-06-08 11:05:41

Affected files:
02. PARA/03. Resources(資源)/C++17/rvalue.md
This commit is contained in:
2022-06-08 11:05:41 +08:00
parent 1bbd66d5ce
commit d89c1072d4

View File

@@ -15,4 +15,14 @@ rvalue reference可以減少無間的複製成本。例如
std::string longStr = "qwertyuiopasdfghjklzxcbnm";
std::string anotherLong = longStr;
```
這時longStr會被複製一份anotherLong把longStr換成很大的buffer
這時longStr會被複製一份anotherLong把longStr換成很大的buffer其複製成本就更大了。用rvalue reference可以消除這個複製成本。如下
```cpp
std::string longStr = "qwertyuiopasdfghjklzxcbnm";
std::string&& anotherLong = longStr;
std::cout << anotherLong; // "qwertyuiopasdfghjklzxcbnm"
//std::cout << longStr; // Undefined bahavior, don't do this
```
上面的例子longStr的內容被「轉移」給anotherLong所以不可以再longStr這時候longStr的內容取決於move operator的作法。
不過