简体   繁体   English

如何编写以通用迭代器为参数的模板化 function

[英]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.

相关问题 如何编写以通用方式获取迭代器或集合的函数? - How to write a function that takes an iterator or collection in a generic way? 采用模板化长度参数的 Function - Function that takes templated length parameter 如何创建一个模板化函数,该函数采用作为 boost::bind 结果的参数? - How can I make a templated function that takes a parameter that is the result of boost::bind? 如何使用以集合为参数的模板化客户端display()函数 - How to use a templated client display() function that takes a set as the parameter 如何为通用接口编写本机模板化后备存储? - How can I write a native templated backing storage for a generic interface? 如何编写一个采用通用C ++标准库容器的模板函数 - How can I write a template function that takes a generic C++ standard library container C ++如何重载以不同模板对象的迭代器作为参数的函数? - C++ How to overload a function which gets as parameter an iterator of different templated objects? 如何编写一个带有默认参数的lambda的泛型函数? - How to write a generic function that takes a lambda with a default argument? 如何使用static_assert检查模板化函数的迭代器参数的元素类型? - How can static_assert be used to check element type of iterator argument to templated function? 如何为子类定义通用模板化创建函数 - How to define generic templated create function for subclass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM