[英]How can one write a templated function that takes a generic iterator as a parameter
I've been writing a templated static class.我一直在写一个模板化的 static class。 For one of the methods, I want to be able to pass in any iterator not just one from a specific data structure like List.对于其中一种方法,我希望能够传入任何迭代器,而不仅仅是来自特定数据结构(如 List)的迭代器。
template <class T>
class Foo
{
public:
template<typename InputIterator>
static bool bar(T&, InputIterator start, InputIterator end);
};
template <class T, typename InputIterator>
bool Foo<T>::bar(T& data, InputIterator start, InputIterator end)
{
typename InputIterator::const_iterator it = start;
while(it != end)
{
//logic here
it++;
}
return true;
}
Above is an example of what I've tried but I keep getting various compiler errors for different variations of this basic design.上面是我尝试过的一个例子,但是对于这个基本设计的不同变体,我不断收到各种编译器错误。
The correct way to define a nested member function template is:定义嵌套成员 function 模板的正确方法是:
template <class T>
template <typename InputIterator>
bool Foo<T>::inRange(T&, InputIterator start, InputIterator end)
{
typename InputIterator::const_iterator it = start;
while(it != end)
{
//logic here
it++;
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.