It appears that the iterator adaptor reverse_iterator
doubly defines most of its nested types. In particular, it inherits publicly from std::iterator
which exposes iterator_category
, value_type
, difference_type
, pointer
and reference
. Except for iterator_category
and value_type
, these are all explicitly typedef
'ed again in the class definition.
24.5.1.1 Class template reverse_iterator [reverse.iterator]
namespace std {
template <class Iterator>
class reverse_iterator : public
iterator<typename iterator_traits<Iterator>::iterator_category,
typename iterator_traits<Iterator>::value_type,
typename iterator_traits<Iterator>::difference_type,
typename iterator_traits<Iterator>::pointer,
typename iterator_traits<Iterator>::reference> {
public:
typedef Iterator iterator_type;
typedef typename iterator_traits<Iterator>::difference_type difference_type;
typedef typename iterator_traits<Iterator>::reference reference;
typedef typename iterator_traits<Iterator>::pointer pointer;
// ... rest of the class
};
Question: why the repetitive definition? Is this just for purposes of exposition, or is there more to it? And why not redefine iterator_category
and value_type
?
For a while now, they've been moving away from using std::iterator
as a base class, and toward just specifying that each iterator must define the correct type names.
When they specify the base class in the standard, that constrains implementations to implement the class that way, even though the only real intent was to specify that the iterator needs to define some names. In particular, you could is_base_of
to determine whether std::iterator
is a base class of std::reverse_iterator
. No, there's nothing polymorphic, so it's pretty silly and pointless to do so, but if you do so, the current standard says it must return true.
It looks (to me) like this is more or less an accidental halfway-point in the process of moving from (more or less accidentally) requiring the use of std::iterator
as a base class, and simply specifying the names that must be defined in std::reverse_iterator
(and various other iterators as well, of course).
For those who care, the history of this includes:
There's also so related discussion papers about deprecating unary_function and binary_function:
These were provided for roughly the same reasons as std::iterator
(i.e., just to provide some typedefs in a derived class) so the reasoning behind removing them is fairly pertinent to ceasing to use std::iterator
as a base class.