c++ - How to check if a template parameter is an iterator type or not? -
template<class t> struct is_iterator { static const bool value = ??? // write ??? }; int main() { assert(false == is_iterator<int>::value); assert(true == is_iterator<vector<int>::iterator>::value); assert(true == is_iterator<list<int>::iterator>::value); assert(true == is_iterator<string::iterator>::value); assert(true == is_iterator<char*>::value); // raw pointer iterator }
the question is: how make 5 assert statements pass?
template<class t> struct is_iterator { static t maket(); typedef void * twoptrs[2]; // sizeof(twoptrs) > sizeof(void *) static twoptrs & test(...); // common case template<class r> static typename r::iterator_category * test(r); // iterator template<class r> static void * test(r *); // pointer static const bool value = sizeof(test(maket())) == sizeof(void *); };
Comments
Post a Comment