I'm wondering why in many template algorithms in the STL the arguments are not passed by reference but rather by value. Here is an example from the <iterator
> header:
template<class InputIterator>
typename iterator_traits<InputIterator>::difference_type
distance (InputIterator first, InputIterator last);
When I pass two iterators to this function, they are copied. My naive thoughts are that it would be better to pass these iterators by const-reference to avoid copying the iterator objects:
template<class InputIterator>
typename iterator_traits<InputIterator>::difference_type
distance (const InputIterator &first, const InputIterator &last);
One could say that iterators are in general very small objects and that copying them is not expensive. But even still: cheap copying would be more expensive than no copying at all.
So what is the reason that in the STL-version, the iterators are passed by value?
Thank you!