Arm*_*yan 13 c++ templates iterator stl template-meta-programming
可能重复:
从迭代器获取const_iterator
我想写一元函数返回相应的const_iterator从iterator
template <class Iterator>
struct get_const_iterator
{
typedef ??? type;
};
Run Code Online (Sandbox Code Playgroud)
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类型.我认为这是一个合理的假设,虽然理论上并不完全正确.
你可以用 C++0x 来做
template <typename Container>
Container container (typename Container :: iterator);
template <typemame Iterator>
struct get_const_iterator
{
typedef decltype (container (Iterator())) :: const_iterator type;
};
Run Code Online (Sandbox Code Playgroud)
尽管我开始同意 Steve 的观点——这不是一个通用的解决方案,因为不同的容器可能具有相同的迭代器类型。
| 归档时间: |
|
| 查看次数: |
1713 次 |
| 最近记录: |