Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The redundancy of forward_iterator concept?

In [iterator.concept.forward], std::forward_iterator is defined as:

template<class I>
  concept forward_­iterator =
    input_­iterator<I> &&
    derived_­from<ITER_CONCEPT(I), forward_iterator_tag> &&
    incrementable<I> &&
    sentinel_­for<I, I>;

and std::sentinel_for<I, I> is defined as:

template<class S, class I>
  concept sentinel_­for =
    semiregular<S> &&
    input_­or_­output_­iterator<I> &&
    weakly-equality-comparable-with<S, I>;

which seems to be redundant in std::forward_iterator's requirement, since input_iterator already models input_or_output_iterator, and incrementable is defined as:

template<class I>
  concept incrementable =
    regular<I> &&
    weakly_­incrementable<I> &&
    requires(I i) {
      { i++ } -> same_­as<I>;
    };

which models regular which models semiregular and equality_­comparable which models weakly-equality-comparable-with<I, I>.

Why does the standard need to add a constraint that seems redundant here, and what are the considerations behind this?

like image 879
康桓瑋 Avatar asked Mar 01 '23 13:03

康桓瑋


2 Answers

forward_­iterator needs the semantic requirements of sentinel_for. Those are not implied by either regular or equality_comparable.

like image 101
T.C. Avatar answered Mar 11 '23 06:03

T.C.


Concepts have explicit syntactical requirements (ie: these expressions have to compile), but they also have implicit semantic concepts. equality_comparable<T> has a semantic requirement that if t == u, then t and u have the same value, however that is defined for T.

But sentinel_for requires a more specific meaning for t == u. Equality between a sentinel and an iterator means that the iterator is at the end of the range (and therefore, you aren't allowed to dereference it).

So these concepts are not redundant; they are needed to express the semantic meaning of forward_iterator<T>. In this case, that testing one iterator against another is a valid way to check for the end of the sequence. And therefore, the meaning of "value" for a forward iterator is "position in the range".

like image 39
Nicol Bolas Avatar answered Mar 11 '23 05:03

Nicol Bolas