Reputation: 105
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?
Upvotes: 1
Views: 911
Reputation: 63471
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 )
{
// ...
}
Upvotes: 2