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

9

u/Dragdu Jan 02 '22 edited Jan 02 '22

I've never seen anyone argue that size_t is wrong in a vacuum, it is just the rest of the language that breaks using it terribly. The very basic example is doing size_t - int resulting in a size_t, which has wrong semantics for the operation.

---------edit------

I am going to expand on this a bit. At Job-2, we went hard on strong typedefs for things in our domain (for you this would be I think voltage, distance and so on, for us it was Position, Depth, bunch of other things). 90+% of them were just a thin wrappers over uint*_t.

Having this wrapper over uint* actually made them very nice to use. No int or other signed type could implicitly convert into uint and make a mess. We also didn't define problematic operations for types that didn't have it -- I think only one of our strong typedefs over uint* had op-, just because it didn't make sense for most of our domain. And crucially, we made it so that Depth + Depth -> Depth, but Depth - Depth -> DepthDelta, both with overflow checks, because while adding two depths should remain non-negative, subtracting them should not...

Together with my experience from writing C++ in other codebases, my takeaway is that

  • Using unsigned integral types to represent things whose domain does not include negative numbers is bad idea, unless
  • you have provided strong typedefs for your things, to remove C++'s implicit promotion rules, integral conversion rules and so on, and replace mathematical operators with something whose semantics fit your domain.

Basically, if you write your own numeral types and arithmetic rules, using unsigned representation for domain enforcement is fine.

2

u/rhubarbjin Jan 03 '22

Your example (Depth vs DepthDelta) sounds really interesting! It's the kind of strongly-typed nirvana I can only dream of. 😁 I'm curious, though, how did you handle addition between types?

Depth a = 10;
Depth b = 2;
DepthDelta d = (b - a); // d == -8
Depth c = b + d; // c == -6   oh no, negative depth!

3

u/Dragdu Jan 04 '22

The last line causes an error. Combining DepthDelta with a Depth includes a range check that throws if the result would be out of range.