当写作“迭代”通用功能范围,我通常做的:推断类型的模板键入C++
template <typename Iter> auto func(Iter &first, Iter &last)
{
using IterType = typename std::decay<decltype(*first)>::type;
...
}
另一种方式似乎是:
template <typename Iter> auto func(Iter &first, Iter &last)
{
using IterType = typename std::iterator_traits<Iter>::value_type;
...
}
然而第三:
template <typename Iter> auto func(Iter &first, Iter &last)
{
using IterType = typename Iter::value_type;
...
}
没有申请iterator_traits
。
从理论上讲,我的函数应该只接收迭代器,如first
和last
,第二种形式理想地(imho)是最常用的方式来获得类型。但是使用typename std::decay<decltype(*first)>::type
这个最通用的习惯用法是为了不对Iter
施加限制,比如定义了一个value_type
?
我认为这是按地理价值传递迭代器。 – Lingxi
如果'iterator_traits'对某些东西不起作用,则说某事不是迭代器。 –