On Thursday, July 4, 2019 at 4:53:34 PM UTC+10, Boris Zbarsky wrote:
> On 7/4/19 10:11 PM, Gerald Squelart wrote:
> > - I found plenty of `unsigned`s around, more than `uint32_t`s.
> 
> How many are in code that predates the ability to use uint32_t, though?

I didn't do deeper archaeology, so it's very possible most unsigneds are in old 
or 3rd party code. But I didn't want to spend more time on this, in case 
old-timers already had knowledge about it. ;-)

> > - Our latest coding style [1] points at Google's, which has a section about 
> > Integer Types [3], and the basic gist is: Use plain `int` for "not-too-big" 
> > numbers
> 
> If you can 100% guarantee that they will not be too big, right?
> 
> (In particular, for generation counters I would be somewhat worried 
> about making such a guarantee.)

They did add "use int64_t for "big" numbers".

In my own selfish case, it will be once per profiler start/stop. I'm fairly 
confident a user won't start and stop the profiler a few billion times in a 
session. :-D

But that was not the point of this discussion, sorry for the distraction.

> > never use any unsigned type unless you work with bitfields or need 2^N 
> > overflow (in particular, don't use unsigned for always-positive numbers, 
> > use signed and assertions instead).
> 
> Do you happen to know why?  Is this due to worries about underflow or 
> odd behavior on subtraction or something?

Details in OP[3]. Some juicy bits:
"Because of historical accident, the C++ standard also uses unsigned integers 
to represent the size of containers - many members of the standards body 
believe this to be a mistake, but it is effectively impossible to fix at this 
point."
"The fact that unsigned arithmetic doesn't model the behavior of a simple 
integer, but is instead defined by the standard to model modular arithmetic 
(wrapping around on overflow/underflow), means that a significant class of bugs 
cannot be diagnosed by the compiler."
"Mixing signedness of integer types is responsible for an equally large class 
of problems."

> -Boris

g.
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to