Out of curiosity, what's the rationale to use a template parameter for std::advance()
's distance type, but use the iterator's difference_type
for the distance in std::next()
and std::prev()
?
Why not use the same approach (either one)?
Follow up:
Presence of default n = 1
does not seem to prevent next
to be templated by Distance
as was suggested in the answer below. This compiles:
#include <iterator>
#include <set>
template<typename InputIt,
typename Distance = typename std::iterator_traits<InputIt>::difference_type>
InputIt my_next(InputIt it, Distance n = 1)
{
std::advance(it, n);
return it;
}
int main()
{
std::set<int> s;
my_next(s.begin());
my_next(s.begin(), 10ul);
return 0;
}