
1.STL萃取机利用Traits技术可以区别迭代器与普通指针
2.STL萃取机利用Traits技术可以获取迭代器内部数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
using namespace std;
template <class > struct iterators { typedef T type; };
template <class > struct traits { typedef typename T::type type; };
template <class > struct traits<T*> { typedef T* type; };
template <class > typename traits<T>::type get(const T&) { return typename traits<T>::type(); }
void fun(int) { cout<<"This is Iterator"<<endl; } void fun(int*) { cout<<"This is Pointer"<<endl; }
int main() { iterators<int> it1; int* it2; fun(get(it1)); fun(get(it2)); return 0; }
|
注:对此文章若有问题请往issue留言(ps:注明title为traits)
近期评论