I would like to use something like typedef in my C++ programs to enhance type safety.
As an example, suppose I have two functions
void function1(unsigned idOfType1);
void function2(unsigned idOfType2);
then I can mistakenly pass idOfType2 to function1 and vice versa. I want the compiler to give me an error in this case. I am aware that I could wrap these unsigned in a struct, but then I'd have to give provide a field name and use .
to access them, which is slightly inconvenient. Is there a good way around this?
Edit: As far as I know typedef
will not work for this purpose as it is just a shorthand for a type and will not be used for type checking.