Sam said:
I don't know of any such library, but writing this kind of a template
should not be very hard.
Really? I see the following problems right away:
a) It is tedious.
b) It is error-prone and you have to provide many tests covering a lot of
corner cases.
c) Deciding how to handle conversions is tricky. Handling them the way you
settled on, is at least as tricky. E.g., should there be conversions
T -> saturating<T> and back ?
If T -> T' is a conversion,
should saturating<T> -> saturating<T'> be allowed ?
You also need to decide which of those conversions should be explicit and
which should be implicit.
d) Do you want to be standard conforming, or is it ok to rely on
implementation specifics? E.g., the standard leaves it to the
implementation to define a/b and a%b for negative operands. If you are
dealting with signed types, you have to test for overflow _before_ the
operation (otherwise, you have UB). For multiplication, that involves
division and can leads to implementation defined behavior if you do not
take special precautions.
e) What about division? If INT_MAX effectively means infinity, what should
INT_MAX/2 be?
Best
Kai-Uwe Bux