getting a const_iterator from iterator

Viewed 1906

Possible Duplicate:
Obtaining const_iterator from iterator

I want to write a metafunction which returns the corresponding const_iterator from an iterator

template <class Iterator>
struct get_const_iterator
{
    typedef ??? type;
};
  • get_const_iterator<int*>::type must be const int*
  • get_const_iterator<const int*>::type must be const int*
  • get_const_iterator<int* const>::type must be const int* or const int* const, I don't care
  • get_const_iterator<std::list<char>::iterator>::type must be std::list<char>::const_iterator

etc.

Can this be done with iterator_traits or without them?

Edit: Let's assume that if 2 container have the same iterator type then they also have the same const_iterator type. I think this is a reasonable assumption, although theoretically not entirely correct one.

2 Answers
Related