Re: [分享] typedef dereference of a type

看板C_and_CPP作者 (我要加入劍道社!)時間14年前 (2010/12/31 03:15), 編輯推噓5(502)
留言7則, 5人參與, 最新討論串2/5 (看更多)
http://www.cplusplus.com/reference/std/iterator/iterator_traits/ 所以這樣寫就可以了 template < typename IterType > class Foo { typedef typename std::iterator_traits<IterType>::value_type ValueType; } ; iterator_traits 的優點是不需要 c++0x 的 decltype 而且它會幫你去掉 const 與 reference 直接用 pointer 當 IterType 也是 OK 的 不過使用者傳入的 IterType 必需依照 STL 的慣例進行定義 也就是 IterType 自己要有 value_type/reference_type 等等的 member type 如果你預設 user 只會傳 pointer 和 STL iterator 那 iterator_traits 是最方便的解法 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.32.15.163

12/31 13:15, , 1F
可惡 果然有這種東西 XD 我就想說沒有的話很奇怪
12/31 13:15, 1F

12/31 14:11, , 2F
XD
12/31 14:11, 2F

01/01 11:03, , 3F
推XD
01/01 11:03, 3F

01/01 13:40, , 4F
其實可以用 Boost.TypeTraits,裡面的東西多到嚇人
01/01 13:40, 4F

01/01 13:41, , 5F
01/01 13:41, 5F

01/01 20:10, , 6F
第一時間就想到iter_traits不過想到用法就..><
01/01 20:10, 6F

01/01 21:55, , 7F
是阿...不適用的要想辦法覆寫掉
01/01 21:55, 7F
文章代碼(AID): #1D7KhMm8 (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1D7KhMm8 (C_and_CPP)