template< class InputIterator, class Distance >
void advance( InputIterator &i, Distance n )
{
if( is_random_access_iterator( I ) ) {
advance_RAI( i, n );
} else if( is_bidirectional_iterator( I ) ) {
advance_BI( i, n );
} else {
advance_II( i, n );
}
}
The disadvantage of this approach is which advance_xx() is going to be used is determined during runtime, not at compile time, and thus the performance is not ideal.
Overloading mechanism can address this problem.
// Define five tag types
struct input_iterator_tag {};
struct output_iterator_tag {};
struct forward_iterator_tag : public input_iterator_tag {};
struct bidirectional_iterator_tag : public forward_iterator_tag {};
struct random_access_iterator_tag : public bidirectional_iterator_tag {};
These classes are only used as tags, and no need any members.
Now, the above advance_xx() functions can be written like below:
template< class InputIterator, class Distance >
inline void __advance( InputIterator &i, Distance n, input_iterator_tag )
{
while( n-- ) ++I;
}
template< class ForwardIterator, class Distance >
inline void __advance( ForwardIterator &i, Distance n, forward_iterator_tag )
{
advance( i, n, input_iterator_tag() );
}
template< class BidirectionalIterator, class Distance >
inline void __advance( BidirectionalIterator &i, Distance n, bidirectional_iterator_tag )
{
if( n >= 0 ) {
while( n-- ) ++i;
} else {
while( n++ ) --i;
}
}
template< class RandomAccessIterator, class Distance >
inline void __advance( RandomAccessIterator &i, Distance n, random_access_iterator_tag )
{
i += n;
}
The third parameter is only used to invoke overloading mechanism.
Now, the interface advance() is much simpler as below:
template< class InputIterator, class Distance >
void advance( InputIterator &i, Distance n )
{
__advance( i, n, iterator_traits< InputIterator >::iterator_category() );
}
In addition, to achieve the above feature, traits needs another type:
template< class I >
struct iterator_traits {
...
typedef typename I::iterator_category iterator_category;
};
// Partial specialisation for raw pointer
template< class T >
struct iterator_traits< T* > {
...
// Note, raw pointer is of RandomAccessIterator
typedef random_access_iterator_tag iterator_category;
};
// Partial specialisation for pointer-to-const
template< class T >
struct iterator_traits< const T* > {
...
// Similarly, pointer-to-const is also of RandomAccessIterator
typedef random_access_iterator_tag iterator_category;
};