From 58178415c40942991d8c970a4b6d0aecc6326cd2 Mon Sep 17 00:00:00 2001 From: Awin Huang Date: Wed, 8 Jun 2022 13:52:41 +0800 Subject: [PATCH] vault backup: 2022-06-08 13:52:41 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Affected files: 02. PARA/03. Resources(資源)/C++17/rvalue.md --- .../03. Resources(資源)/C++17/rvalue.md | 20 +++---------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/02. PARA/03. Resources(資源)/C++17/rvalue.md b/02. PARA/03. Resources(資源)/C++17/rvalue.md index f98875c..38b6c14 100644 --- a/02. PARA/03. Resources(資源)/C++17/rvalue.md +++ b/02. PARA/03. Resources(資源)/C++17/rvalue.md @@ -9,20 +9,6 @@ int c{5}; int&& rtepm {c + 3}; ``` -關於lvalue、rvalue的分別可以參考[Value categories - cppreference.com](https://en.cppreference.com/w/cpp/language/value_category),或是[rvalue 參考](https://openhome.cc/Gossip/CppGossip/RvalueReference.html)等文章。這邊講用法跟好處。 -rvalue reference可以減少無間的複製成本。例如: -```cpp -std::string longStr = "qwertyuiopasdfghjklzxcbnm"; -std::string anotherLong = longStr; -``` -這時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的作法。 - -不過 \ No newline at end of file +## 參考 +- [Value categories - cppreference.com](https://en.cppreference.com/w/cpp/language/value_category) +- [rvalue 參考](https://openhome.cc/Gossip/CppGossip/RvalueReference.html) \ No newline at end of file