Forward Iterator Output-Iterator Iterators?

Does ForwardIterators need to be OutputIterators? My current STL implementation (VS2012) outputs forward_iterator_tag from input_iterator_tag and output_iterator_tag , but I cannot find this requirement in the standard [N3485].

+7
source share
1 answer

In C ++ 11, no, for repeated iterators, iterators are not required to be output. The requirements of the output iterator are similar to the additional sets of requirements that the iterator can have, regardless of the other requirements of the iterator that it encounters. Forward iterators only need input iterators (Β§24.2.5 / 1):

An X class or pointer type satisfies the requirements of an advanced iterator if:

  • X satisfies the requirements of the input iterator
  • ...

In fact, a direct iterator satisfies the requirements of the output iterator only if it is a mutable iterator for a sequence of types assigned by copy † .

† or a constant iterator to a sequence of types with operator=(...) const defined with mutable members.

To a greater extent, iterator tags are defined by the standard as (Β§24.4.3 / 2):

 namespace std { 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 { }; } 

As you can see, forward_iterator_tag should inherit only from input_iterator_tag .


C ++ 03 states that forward iterators satisfy the requirements of input and output iterators:

Forward iterators satisfy all the requirements of input and output iterators and can be used whenever any kind is specified.

But in the next paragraph, this contradicts the assertion that a constant forward iterator does not satisfy the requirements for output iterators:

In addition to its category, the direct, bidirectional, or random access iterator can also be variable or constant depending on whether the result of the * i expression leads as a reference or as a reference to a constant. Constant iterators do not satisfy the requirements for output iterators, and the result of the * i expression (for the constant iterator i) cannot be used in an expression where an lvalue is required.

However, the definition of iterator tags is identical to the definition in C ++ 11. In this contradictory wording there was a defect report , but it was closed as "Not a defect" because the first quote is in the "introductory text" of the section and is likely to be in the future ( What was it).


The SGI definition of the forward iterator is given as a refinement of the input and output iterators (thanks @BenVoigt in the comments).

However, if we look at the implementation of iterator tags, we find that forward_iterator_tag still inherits only from input_iterator_tag .

It seems like this was a confusing area in the past, but if VS2012 defines forward_iterator_tag as inheriting from output_ - and input_iterator_tag , I can only assume that this is an error.

+11
source

All Articles