[問題] 關於boost::ref()的疑問?

看板C_and_CPP作者 (mimi)時間14年前 (2010/04/27 14:04), 編輯推噓0(004)
留言4則, 2人參與, 最新討論串1/1
目前正在練習boost的一些範例 有關理解boost的實做上有點疑問 範例是這樣~ struct TEST { int k; }; template <typename T> int foo(T a) { typedef boost::unwrap_reference<T>::type& truetype; truetype k = a; return 0; } int main { TEST test1; foo(boost::ref(test1)); } 有問題的部份是 這一段code typedef boost::unwrap_reference<T>::type& truetype; 根據範例的code 這段依該是會complie成 boost::unwrap_reference<reference_wrapper<TEST> >::type& truetype; 根據boost的原始碼 ref.hpp template<class T> class reference_wrapper { public: typedef T type; ... ... } template<typename T> class unwrap_reference { public: typedef T type; }; 我認為 truetype 這個型別應該是 reference_wrapper<TEST>& 但是執行結果卻是 TEST& 這個型別 感覺上 reference_wrapper<TEST>& 又偷偷轉成 reference_wrapper<TEST>::type& 想問一下為什麼型別是變成 TEST& 而不是我想的 reference_wrapper<TEST>& 搞不清楚的問題 想問問有研究的版眾~ 感謝大家~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.120.189.231

04/27 23:46, , 1F
unwrap_reference 不是這樣做的, 網頁上有說如果參數 T 是
04/27 23:46, 1F

04/27 23:47, , 2F
reference_wrapper 結果會是 T::type 否則就是單純的 T
04/27 23:47, 2F

04/28 00:12, , 3F
請問樓上的~你說網頁上有寫~可以參考一下嗎? 感謝~
04/28 00:12, 3F

04/28 00:25, , 4F
04/28 00:25, 4F
文章代碼(AID): #1BrdvO9u (C_and_CPP)