How to determine the iterator type for container?

c++, type-traits

Solution

If you just want to separate containers from pointers, you can try this

template<class T>
struct iterator_for 
{
    typedef typename T::iterator  type;
};

template<class T>
struct iterator_for<T*>
{
    typedef T*  type;
};

template<class T, std::size_t N>
struct iterator_for<T (&)[N]>
{
    typedef T*  type;
};

Problem

What do I have to write for YYY, ZZZ in order to set `iterator_type` to the iterator type associated with `T` ? It should work in Visual Studio C++ 2010, if possible (but general standard solution is ok as well). ``` template<class T> struct iterator_for { typedef YYY<T>::ZZZ type; } ``` Hence I want: ``` iterator_for<double[3]>::type is double * iterator_for<std::string>::type is std::string::iterator iterator_for<char[12]>::type is char * ``` etc. I have a templated wrapper class `Wrapper<T>` storing something iterable (i.e. a container or string or an array) and I want to define a function returning an iterator pointing into the wrapped object. For that purpose, I need to be able to speak about the iterator type corresponding to `T`. For an array, the corresponding iterator would be a pointer, and for a string that is whatever string defines to be its iterator type.

Original source