Is there in C, or at least in GCC, some way of making (with typedef) a type that is incompatible with any other type?
For example, you make:
typedef UINT UID;
typedef UINT AGE;
UID user_id;
AGE user_age;
You can see that both types are unsigned int (I name it UINT).
You can calculate user_id + user_age.
BUT, you want to be sure UIDs and AGEs will never be mixed.
That's what I want!
The idea is for the security and correctness of the code, to specify some qualifier/attribute for some types.
Than, the ONLY way to mix them will be casting both to UINT, or casting user_age to UID, for example.
C language can be very confuse, and sometimes we take HOURS just to find out that silly bug is there just because you used the wrong value as an argument because the variables have similar names... and the compiler obviously will never complain, because they have the same type.
I didn't find any atttribute for this in the GCC manual, but I'm going to request it in the mailing list.
I just want to know HOW (and I know there isn't, the real question I have is WHY the standard and the compiler doesn't provide this, as I see it as really useful and relatively one of the most easy things to implement at the compiler side) to specify some ATTRIBUTE on a type (and also on variables) to tell the compiler that TYPE is not supposed to be mixed with any other, unless explicitly casted to it. So the real question here is: - Why is this a bad idea? (Why no compiler consider it?) - Would you also use it if this GCC attribute existed? Oh... in my opinion I would use it here and there, just put this attribute in almost all typedefs and then just program; a HUGE part of the mistakes would be detected at the first compilation - passing arguments in the wrong order, using the wrong variable in a big and complicated calculation...
Sorry for my bad english.