[問題] unique_ptr與move
問題(Question):
https://www.chromium.org/rvalue-references
在上面這個網頁裡,看到這段敘述
However, if the types of the variable and the return type do not match
exactly, then you will have to use std::move() in order to convert without
creating an extra temporary.
std::unique_ptr<MyType> MakeMyType()
{
std::unique_ptr<ChildClassOfMyType> ptr;
// This call to std::move() is needed to convert from a pointer-to-the
// child class to a pointer-to-the parent.
return std::move(ptr);
}
我看不懂的是…為什麼這樣寫可以減少extra temporary呢?
如果不這麼寫的話又會造成什麼問題?
(想知道這個寫法的反例?)
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.25.206
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1470842731.A.732.html
→
08/10 23:40, , 1F
08/10 23:40, 1F
→
08/10 23:40, , 2F
08/10 23:40, 2F
→
08/10 23:43, , 3F
08/10 23:43, 3F
→
08/10 23:43, , 4F
08/10 23:43, 4F
→
08/10 23:45, , 5F
08/10 23:45, 5F
→
08/10 23:46, , 6F
08/10 23:46, 6F
推
08/11 00:54, , 7F
08/11 00:54, 7F
靠這篇我有推結果自己完全沒消化 Q_Q
推
08/11 01:02, , 8F
08/11 01:02, 8F
推
08/11 03:47, , 9F
08/11 03:47, 9F
→
08/11 03:47, , 10F
08/11 03:47, 10F
推
08/11 03:51, , 11F
08/11 03:51, 11F
→
08/11 03:51, , 12F
08/11 03:51, 12F
→
08/11 03:51, , 13F
08/11 03:51, 13F
→
08/11 03:51, , 14F
08/11 03:51, 14F
→
08/11 03:52, , 15F
08/11 03:52, 15F
推
08/11 03:53, , 16F
08/11 03:53, 16F
→
08/11 03:53, , 17F
08/11 03:53, 17F
→
08/11 03:53, , 18F
08/11 03:53, 18F
推
08/11 04:48, , 19F
08/11 04:48, 19F
→
08/11 04:48, , 20F
08/11 04:48, 20F
→
08/11 13:34, , 21F
08/11 13:34, 21F
推
08/11 14:21, , 22F
08/11 14:21, 22F
推
08/11 14:26, , 23F
08/11 14:26, 23F
等等一樓講完了嗎我還不明白啊 Q_Q
推
08/12 20:11, , 24F
08/12 20:11, 24F
最近一直在看相關的資料,包括yoco那篇文
但我發現自己連"會不會產生暫時物件"都有點分不清楚orz
繼續唸書.......
推
08/12 21:34, , 25F
08/12 21:34, 25F
感謝!!!!
※ 編輯: james732 (223.137.25.206), 08/12/2016 22:24:51
→
08/13 21:33, , 26F
08/13 21:33, 26F