Confusion over semantics / conceptual requirements of InputIterator

C ++ InputIterator is one of the most limited types of Iterator Concept. It is only guaranteed to support dereferencing, equality comparison, pre-increment and post-increment (and post-increment and dereference)

Since InputIterator objects InputIterator often repeated over an arbitrary stream, you cannot even be sure that iterating twice over the same input will give the same value.

I'm confused, however, if the dereferencing operator * guaranteed to return the same value every time you play it, provided that you never increase the iterator.

For example, if std::begin(some_input_stream) returns an object that meets the requirements of the InputIterator concept, and it not equal or is located beyond the end position:

 auto it = std::begin(some_input_stream); auto value1 = *it; auto value2 = *it; assert(value1 == value2); 

Is value1 guarantee of the same value as value2 ? (Providing, of course, that any type of *it provides implements the semantics of comparing semantic equality)

+6
source share
2 answers

Is value1 guarantee of the same value as value2 ?

Yes. In fact, you can also copy the iterator, and this copy is guaranteed to give the same result until you increase one of the iterators:

 auto it2 = it; auto value3 = *it2; assert(value3 == value1); ++it2; auto value4 = *it; // ERROR: might not be dereferencable any more 

This is indicated by the requirements for *a in C ++ 11 Table 107 (Requirements for the input iterator):

If a == b and (a,b) is in the region == , then *a equivalent to *b .

and after ++r :

Any copies of the previous r value are no longer required to be legible or to be in the == domain.

+9
source

I believe that it should work. The requirements placed on the InputIterator effectively force the implementation to cache the last read value, which is then returned by operator* .

0
source

Source: https://habr.com/ru/post/953484/


All Articles