Keeping types safe

I have a function which is passed an unsigned integer. The value of the integer must be zero or ( 1 << 31 ). In order to ensure this happens, I've done this:
1
2
enum free_edge { USED = 0, FREE = 1 << 31 };
void myFunc(free_edge);


Another function, also taking an unsigned integer, has a less strict range. Namely, it has to be less than 1 << 30.

I can of course check to make sure it's valid every time, but I've thought perhaps better to do this:
1
2
3
4
5
6
struct size_in_bytes{
  uint32_t size;

  size_in_bytes(uint32_t s){
    if ( s >= ( 1 << 30)) size = (1 << 30) - 1;  // maybe I should throw?
    else                  size = s;


I guess the question is, should I make a Ranged_Int class?
Last edited on
Topic archived. No new replies allowed.