c++functiontemplatesiteratortemplate-function

how to define template function that takes two iterators in c++?


I would like to define a template function that takes two iterators, one being begin() and another being end(). How can this be achieved in c++?

So far, I can think of the following:

template <class Iterator>
typename std::iterator_traits<Iterator>::value_type func( Iterator begin, Iterator end ) {

}

Is this wrong?


Solution

  • The way the standard library tends to work (at least taking <algorithm> as an example) is to allow type resolution to happen later. So you would instead use:

    template <class InputIt, class T>
    T func( InputIt begin, InputIt end )
    {
        // ...
    }