C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Signed sizes

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Wed, 11 Dec 2024 11:15:17 +0000
On Wed, 11 Dec 2024 at 10:55, Tiago Freire <tmiguelf_at_[hidden]> wrote:

>
>
> > [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.
>
>
>
> Look I’m not trying to be patronizing here. Unsigned integer arithmetic is
> a well behaved and well-defined thing, much more so than signed is.
>
> And there’s no reason to assume that E1[E2] = *((E1)+(E2))" in c-arrays
> must mean that the addition is using “signed arithmetic rules” as opposed
> to “unsigned arithmetic rules”. Not that it would make any difference since
> on almost all platforms there’s absolutely no distinction between signed
> and unsigned types when it comes to arithmetic, the CPU has no knowledge on
> signdness, the arithmetics are the same regardless of either you think the
> result came a about because of a genuine signed distinction or a unsigned
> wrap-around behavior, conversion between signed an unsigned values of the
> same size is a no-op.
>
>
>
> What does change is variable promotion rules (which are broken) where in
> signed types you have to do a non-trivial 1’s fill, and comparisons which
> is worse for signed types because they don’t represent the right thing.
>
>
>
> It may not be trivial to think about this in these terms, but if you do
> there’s no confusion to be had.
>

Who said they're confused? I certainly didn't.

All I said was that I've used negative indices with pointers and other
random access iterators. A quick visit to Debian code search shows I'm not
alone.

As Oliver said, there's no objectively right answer, and no clear
consensus, but just insisting that people are wrong to weigh the trade-offs
differently isn't helpful. There are valid arguments for preferring signed
and for preferring unsigned. We should accept we need to make a trade-off
and then aim to reduce unsafe traps that people can fall into.

Received on 2024-12-11 11:16:33