On Wed, 11 Dec 2024 at 08:31, Tiago Freire via Std-Proposals <std-proposals@lists.isocpp.org> wrote:

 

Jonathan Wakely wrote:

> Always? I've definitely used iter[-1] when dealing with random access iterators and with pointers into arrays. It's more concise than *std::prev(iter), especially if other code nearby is using iter[0] and iter[1].

 

An iterator is not a container.


Thank you for this insight. I was replying to an email that was talking about iterators though.

 

While there are reasons to makes sense to used signed values with random access iterators, the fact that you can use iterators in a such a way is a whole set of other problems that I think are of topic for this discussion.


iter[-1] is equivalent to *(iter + -1) and therefore valid and a demonstration of why indices on iterators (including pointers) should use the difference_type, which should be signed.

 

 

Thiago Macieira wrote:

> But you cannot say the same about the distance between two arbitrary elements in a sequence. You don't know the direction, therefore you must record it. You could just record it with an unsigned and a direction, but that would make APIs like operator+ impossible because of the inability to specify direction.

> In any case, the language says pointer distances are ptrdiff_t and thus signed.

 

I agree, distances between two arbitrary points should be signed. I don’t see what the confusion is here. A distance is not the same thing as an index.


[expr.sub]:
"The expression E1[E2] is identical (by definition) to *((E1)+(E2))"

Your argument amounts to just repeating "but it can't be negative so it should be an unsigned type" again and again with different words, and trying to patronise people suggesting maybe it's too difficult for them to understand. It's not an effective approach.