C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Signed sizes

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Wed, 11 Dec 2024 10:55:13 +0000

> [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.

Received on 2024-12-11 10:55:19