可能重复:
从迭代器中获取const_iterator
我想编写一个元函数,它可以从一个iterator
返回对应的const_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
完成吗?还是不需要它们?
编辑:假设如果两个容器具有相同的iterator
类型,则它们也具有相同的const_iterator
类型。我认为这是一个合理的假设,尽管在理论上并不完全正确。
vector<int>::iterator
和list<int>::iterator
可以是同一个类(这对于未能满足单一职责原则来说如何?),但vector<int>::const_iterator
和list<int>::const_iterator
可以是不同的类。 - Steve Jessopconst_iterator
所使用的实际类型,能解决实际问题吗?事实上,你可能需要多个包装器,因为你希望根据迭代器标签进行调度。 - Steve Jessop