Re: [問題] unique_ptr與move、type conversion、return statement

作者: Caesar08 (Caesar)   2016-08-13 23:27:58
※ 引述《james732 (好人超)》之銘言:
: 標題: [問題] unique_ptr與move
: 時間: Wed Aug 10 23:25:24 2016
:
: 問題(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呢?
: 如果不這麼寫的話又會造成什麼問題?
: (想知道這個寫法的反例?)
:
:
作者: firose (guest也是也是也是也是也)   2016-08-13 23:55:00
由此看來 extra object 應該是 (2) implicit 建構的物件關鍵點還是在 c++14 把它先視為 rvalue @@說錯了 @@ 應是(1)產生物件 然後再(2)move回去如果加 move 就直接 2 回去
作者: Sidney0503 (Sidney0503)   2016-08-14 09:48:00
自己發文自己回 教你啦 開分身回文比較不孤單
作者: james732 (好人超)   2016-08-14 15:20:00
推,好像有點懂了
作者: bluesoul (忙死你老爸)   2016-08-15 23:11:00
move可能會破壞RVO
作者: g0010726 (Kevin)   2016-08-16 00:05:00
你說的好像是這條standard, 我不是很確定http://goo.gl/kTSQDp

Links booklink

Contact Us: admin [ a t ] ucptt.com