看板 C_and_CPP 關於我們 聯絡資訊
目前正在練習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
firose:unwrap_reference 不是這樣做的, 網頁上有說如果參數 T 是 04/27 23:46
firose:reference_wrapper 結果會是 T::type 否則就是單純的 T 04/27 23:47
fuha:請問樓上的~你說網頁上有寫~可以參考一下嗎? 感謝~ 04/28 00:12
firose:http://ppt.cc/97MP http://ppt.cc/tw!5 最下面 04/28 00:25