Has anyone else (in C) wrapped an int type in a struct to avoid accidental mixing of ints with different semantic values?
@jbk Typically we use typedefs to provide a semantic distinction. This is sufficient for the programmer. I kinda like your idea for telling the compiler as well.
On a similar vein I have been thinking about true and false. Are they not the boolean equivalent of magic numbers? I think it would be a good idea to replace them all with constants.
@ocratato The problem is if you have something like:
typedef int foo_t;
typedef int bar_t;
void f(foo_t a, bar_t b) { .. }
foo_t myfoo = 1;
bar_t mybar = 2;
f(mybar, myfoo);
Compiles just fine.
That's why I was thinking about wrapping it in a struct -- you can still pass by value, but get an error if you mess something like the above up.