Boost logo

Boost :

Subject: Re: [boost] Current Guidance on Compiler Warnings?
From: Peter Dimov (lists_at_[hidden])
Date: 2018-11-27 00:24:40


Gavin Lambert wrote:

> In this case for any non-massive array you're fairly safe (esp. when
> someone fixes the call site) with a simple:
>
> assert(i < size());

That is correct. If you have an upper bound, `i < n` for unsigned `i` is
equivalent to `i >= 0 && i < n` with a signed `i`, so in this specific case
you can use either.

Signed is still preferable though because it's less surprising. `x > -1`
gives you the normal answer, for instance. But, you'll say, this will be a
warning. Well yes, this specific case will be, but not all will. Some
signed/unsigned mismatches don't warn on purpose because there are too many
false positives (https://godbolt.org/z/c1rzjS), and in some cases, such as
with the difference between (a-b)/(c-d) and (b-a)/(d-c), unsigned finds a
way to ruin your day without any signed/unsigned mismatches at all.

So it's a long-standing guideline to never use `unsigned`, except for
bitwise operations and modular arithmetic. For numbers, signed is the way to
go.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk