作者littleshan (我要加入劍道社!)
看板C_and_CPP
標題Re: [分享] typedef dereference of a type
時間Fri Dec 31 11:15:31 2010
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
推 yoco315:可惡 果然有這種東西 XD 我就想說沒有的話很奇怪 12/31 13:15
推 loveme00835:XD 12/31 14:11
推 VictorTom:推XD 01/01 11:03
推 avhacker:其實可以用 Boost.TypeTraits,裡面的東西多到嚇人 01/01 13:40
推 tomap41017:第一時間就想到iter_traits不過想到用法就..>< 01/01 20:10
→ loveme00835:是阿...不適用的要想辦法覆寫掉 01/01 21:55