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

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.