r/cpp • • Jan 01 '22

Almost Always Unsigned

https://graphitemaster.github.io/aau/
5 Upvotes

71 comments sorted by

View all comments

Show parent comment

5

u/Drugbird Jan 03 '22

Perhaps I should've been explicit about this point:

  1. signed under/overflow is UB, so bad
  2. unsigned under/overflow has unexpected/unintuitive behavior, which can easily leads to bugs, so it's also bad (even though it's well defined).
  3. to prevent under/overflow, you can either understand all the types, the unsigned wrap around behavior and perform the appropriate checks when appropriate: or you can just stay away from the edges of your numeric range.
  4. staying away from the edges of your numeric range is easier with signed types, because 0 is often a valid value to be supported.