Подтвердить что ты не робот

Получение const_iterator из итератора

Возможный дубликат:
Получение const_iterator с итератора

Я хочу написать metafunction, который возвращает соответствующий const_iterator из iterator

template <class Iterator>
struct get_const_iterator
{
    typedef ??? type;
};
  • get_const_iterator<int*>::type должен быть const int*
  • get_const_iterator<const int*>::type должен быть const int*
  • get_const_iterator<int* const>::type должен быть const int* или const int* const, мне все равно
  • get_const_iterator<std::list<char>::iterator>::type должен быть std::list<char>::const_iterator

и др.

Можно ли это сделать с помощью iterator_traits или без них?

Изменить: Предположим, что если 2 контейнера имеют одинаковый тип iterator, то они также имеют одинаковый тип const_iterator. Я думаю, что это разумное предположение, хотя теоретически не совсем правильно.

4b9b3361

Ответ 1

Вы можете сделать это в С++ 0x

template <typename Container>
Container container (typename Container :: iterator);

template <typemame Iterator>
struct get_const_iterator
{
    typedef decltype (container (Iterator())) :: const_iterator type;
};

Хотя я начинаю соглашаться со Стивом - это не общее решение, так как разные контейнеры могут иметь одинаковые типы итераторов.

Ответ 2

Вы можете сделать это в текущем стандарте, если вы хотите частично специализироваться на контейнерах, как в...

#include <vector>
#include <list>
#include <iterator>

// default case
template <typename Iterator, typename value_type, typename container_test = Iterator>
struct container
{
  typedef Iterator result;
};

// partial specialization for vector
template <typename Iterator, typename value_type>
struct container<Iterator, value_type, typename std::vector<value_type>::iterator>
{
  typedef typename std::vector<value_type>::const_iterator result;
};

// partial specialization for list, uncomment to see the code below generate a compile error
/* template <typename Iterator, typename value_type>
struct container<Iterator, value_type, typename std::list<value_type>::iterator>
{
  typedef typename std::list<value_type>::const_iterator result;
}; */

// etc.

template <typename Iterator>
struct get_const
{
  typedef typename container<Iterator, typename std::iterator_traits<Iterator>::value_type>::result type;
};

int main(void)
{
  std::list<int> b;
  b.push_back(1);
  b.push_back(2);
  b.push_back(3);
  get_const<std::list<int>::iterator>::type it1 = b.begin(), end1 = b.end();
  for(; it1 != end1; ++it1)
    ++*it1; // this will be okay

  std::vector<int> f;
  f.push_back(1);
  f.push_back(2);
  f.push_back(3);

  get_const<std::vector<int>::iterator>::type it = f.begin(), end = f.end();
  for(; it != end; ++it)
    ++*it; // this will cause compile error

}

Конечно, повторит вышеописанный пункт Стив, а также требование состоит в том, что iterator_traits для вашего итератора существует.