Type safe limited range numbers?

While developing (in particular the decoding/encoding methods for the Mch RAW data) I often end up using integers of some width (uint8_t, uint16_t, …) to keep numbers that obviously fit within that width, but also are more limited that the full range. So I often have to add code to x-check that the number I get in some function/method are indeed within the range I’m expecting. For instance a linkId from 0.11 I store in a uint8_t, so when getting a function that takes an uint8_t that is supposed to be a linkId, I have to check it is indeed below 12.

I suspect that is a very general problem and I was wondering if someone (e.g. our meta-programming experts :wink: ) had found an elegant (and performant) solution maybe ?

Thanks,

You google for “dependent types in C++” which will give you a few ideas. I am not sure if I would go completely that route, given the amount of magic those solutions often imply.