There are no virtual methods, because they should not be used polymorphically (in the general sense of polymorphism at runtime), but rather through patterns (static polymorphism). A general approach is a function that accepts iterators templatized to an iterator type. You can find many examples in the header of the STL algorithm:
template <class InputIterator, class Distance> inline void distance(InputIterator first, InputIterator last, Distance& n);
In your particular case:
template <class ForwardIterator>
An additional restriction that it must refer to strings can be implemented in terms of type traits and enable_if:
template <class ForwardIterator> std::enable_if< std::is_same< std::string, typename iterator_traits<ForwardIterator>::value_type > > void acceptIterators( ForwardIterator start, ForwardIterator end );
Using typetraits from C ++ 0x, but enable_if and is_same are available in boost if you need them in a compiler that doesn't have support for them.
If you need to switch the types of iterators at runtime, you may need to look at any_iterator , which any_iterator styles on specific iterators that provide a polymorphic interface at runtime. In the article "On the tension between object-oriented and general programming in C ++", an implementation can be found in Adobe STLab .
source share