Re: [問題] reference vs lifetime of a temporary object

看板C_and_CPP作者 (Caesar)時間7年前 (2016/09/29 21:06), 7年前編輯推噓6(607)
留言13則, 6人參與, 最新討論串1/1
※ 引述《klsdf (靜雨澪)》之銘言: : 標題: [問題] 右值參照問題 : 時間: Wed Sep 28 21:20:59 2016 本文主要是講lifetime of a temporary object,所以把回文的標題改了 我在你的內文說到"reference不管是lvalue reference還是rvalue reference" "都不會延長被reference的object的生命週期" 其實這是錯誤的(抱歉,我誤導你了 <(_ _)> ) 然後板上又沒有相關的資料(查temporary/暫時/生命/lifetime),所以就寫這篇文章 另外,先說一件跟本文章沒關係的事情 以我的經驗,C++ 3大compiler(GCC、Clang、MSVC) MSVC(也就是VC++)是最不符合C++標準的 也就是你的code明明可以通過MSVC的compile,卻不能通過GCC與Clang的compile 讓你以為GCC跟Clang怎麼那麼爛 但實際的情況是MSVC本身有問題,GCC跟Clang才是盡可能符合C++標準的 所以如果當你的code沒辦法通過GCC與Clang的compile,別懷疑,是你的code有問題 正文開始: 實際上,reference(如果沒有特別說明,下文的reference都包含lvalue reference與 rvalue reference,把reference to const改成rvalue reference也可以) 是會延長temporary object的lifetime的,除了3種情況 先講一下甚麼是延長lifetime,如果寫 int test(){ return 10; } int main(){ const int &i=test(); //... cout<<i<<endl; } 原本在const int &i=test()結束,會destroy temporary object的動作,被挪到 i的lifetime結束才執行,所以cout會正確無誤地,把10印出去 這種情況,就是temporary object的lifetime被延長了 但是呢,並不是所有reference都能延長temporary object,例如(其中1種例外情況) string get(){ return "abc"; } struct Test{ const string &str; //oops, dangling reference Test(const string &a) :str{a}{} }; int main(){ Test test(get()); } 那當test(get())結束時,Test::Test的parameter a所reference的object就被destroy了 也就是說,test裡面的str,就會是一個dangling reference 可能有人會問為甚麼 "test.str reference a,a又reference get()的return value" "所以get()的return value(temporary object)被a延長,a又被str延長,不行嗎?" 答案是不行 因為雖然Test::Test的parameter是reference,但 延長temporary object lifetime的規則,在"function的parameter是reference時"無效 所以test.str真的會是一個dangling reference 最後,講那麼多,還是要來回答你原本的問題 : 但就我的認知如果是直接用const auto& =test() 去接了話 : 可以活在do while一個round裡(用VC run的結果是這樣) : 但用DerivedRef接看起來是下一行就結束週期 : 這就是我不瞭解的地方 因為現在用const auto &a=test(),所以test()的return value(temporary object) 的確被a延長了 所以在while結束的時候,temporary object才會被destroy 但是,DerivedRef的案例不一樣 首先,你的Any template(http://codepad.org/XT2ed7Hc)的assignment operator 並沒有test這個class type,有的只是 operator=(const Any& a)與operator=(Any&& a) 也就是說,在你的code(http://codepad.org/CZhy27AQ)40行 "any = test();" test()產生的temporary object,需要先轉成Any這個class type 他會先呼叫template<typename U>Any(U&& value)這個constructor 然後此時的value雖然是reference,但是他是延長temporary object的例外情況 所以當any = test()執行結束後,ptr->value就是一個dangling reference : → firose: DerivedRef 就算被 const T& 也不能活超過 do-block 09/29 12:4 : → firose: 說錯, 是 test(), 只是這裡問題是它能活多久? 09/29 12:4 它只能活到any = test()這個expression結束 : 我的疑問是它太早死了QQ 它沒有太早死,它死的時機剛剛好 註: 在文章一開頭有說到MSVC不符合C++標準的問題,現在這裡就是了 在(http://codepad.org/XT2ed7Hc)裡struct DerivedRef的T& value這種寫法 在遇到any = test()就會出錯,因為你用lvalue reference reference rvalue reference 這段code拿去給GCC跟Clang compile,會發現他們兩個都跟你說compile失敗 不是GCC跟Clang有問題,真正有問題的是MSVC -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.114.233.71 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1475154363.A.F23.html

09/29 21:42, , 1F
yoco
09/29 21:42, 1F

09/29 22:06, , 2F
09/29 22:06, 2F

09/29 23:33, , 3F
...
09/29 23:33, 3F
我有看到你的 暫時物件的生命周期 但我覺得跟本文章內容討論的有點不同,所以才寫這個的啦

09/30 07:13, , 4F
09/30 07:13, 4F

09/30 08:43, , 5F
感謝Caesar大的解釋,我昨天下班的時候有看你貼的連結
09/30 08:43, 5F

09/30 08:43, , 6F
我看完的那時候的理解是原本test()誕生時是rvalue
09/30 08:43, 6F

09/30 08:44, , 7F
被const auto & = test()後成為具名的rvalue 就是lvalue
09/30 08:44, 7F

09/30 08:44, , 8F
所以會等const auto &的scope結束後才會delete
09/30 08:44, 8F

09/30 08:46, , 9F
而我的DerivedRef內的const T&把test()rvalue ref進來時
09/30 08:46, 9F

09/30 08:47, , 10F
並沒有被Compiler辨識出為具名的rvalue,我原本也是理解
09/30 08:47, 10F

09/30 08:49, , 11F
因為是函數呼叫關係導致Compiler無法推導出它是具名rvalue
09/30 08:49, 11F

09/30 08:50, , 12F
經由這篇文章我可以理解為什麼了,謝謝Caesar大。
09/30 08:50, 12F

09/30 11:41, , 13F
好文
09/30 11:41, 13F
※ 編輯: Caesar08 (140.114.233.71), 12/02/2016 00:26:16
文章代碼(AID): #1NxH6xyZ (C_and_CPP)