Date: Wed, 11 Dec 2024 00:49:48 -0300
On Tuesday 10 December 2024 19:42:05 Brasilia Standard Time Tiago Freire
wrote:
> > Then I raise a different and more relevant case than negative indices:
> > distances. All of the iterator and pointer arithmetic must result in
> > distances and those must be signed. The size of a container is nothing
> > but the distance from the beginning to the end and the index of an
> > element is the distance from the beginning to that element.
> No. Because in a distance between the beginning and the end, the beginning
> always comes before the end. You can do arithmetic with unsigned types,
> it's even more consistent than with signed types.
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.
My point is simply that unsigned is a choice but signed is inevitable. I'm not
begrudging the choice and it is not going to change.
And v[-1] is perfectly acceptable in some situations because that's a distance
parameter, not an index. For examples:
constexpr reference back() noexcept
{ return end()[-1]; }
https://codebrowser.dev/quantlib/include/c++/11/bits/
stl_iterator.h.html#__gnu_cxx::__normal_iterator::operator[]
https://github.com/llvm/llvm-project/blob/
77b6910b272c254ad7dd96420a7cc41d917b8a9e/libcxx/include/__iterator/
wrap_iter.h#L99-L101
https://github.com/microsoft/STL/blob/
89ca073a866d24db42b3f4c5e406be4b28a148f4/stl/inc/vector#L1968-L1975
wrote:
> > Then I raise a different and more relevant case than negative indices:
> > distances. All of the iterator and pointer arithmetic must result in
> > distances and those must be signed. The size of a container is nothing
> > but the distance from the beginning to the end and the index of an
> > element is the distance from the beginning to that element.
> No. Because in a distance between the beginning and the end, the beginning
> always comes before the end. You can do arithmetic with unsigned types,
> it's even more consistent than with signed types.
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.
My point is simply that unsigned is a choice but signed is inevitable. I'm not
begrudging the choice and it is not going to change.
And v[-1] is perfectly acceptable in some situations because that's a distance
parameter, not an index. For examples:
constexpr reference back() noexcept
{ return end()[-1]; }
https://codebrowser.dev/quantlib/include/c++/11/bits/
stl_iterator.h.html#__gnu_cxx::__normal_iterator::operator[]
https://github.com/llvm/llvm-project/blob/
77b6910b272c254ad7dd96420a7cc41d917b8a9e/libcxx/include/__iterator/
wrap_iter.h#L99-L101
https://github.com/microsoft/STL/blob/
89ca073a866d24db42b3f4c5e406be4b28a148f4/stl/inc/vector#L1968-L1975
-- Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org Principal Engineer - Intel DCAI Platform & System Engineering
Received on 2024-12-11 03:49:56