Is there any way in C, or at least GCC, to create a (with typedef ) type that is incompatible with any other type?
For example, you do:
typedef UINT UID; typedef UINT AGE; UID user_id; AGE user_age;
You can see that both types are unsigned int (I call it UINT).
You can compute user_id + user_age.
BUT, you want to be sure that the UID and AGE will never be mixed.
This is what I want!
The idea is to ensure the security and correctness of the code, to indicate some attributes / attributes for some types.
Moreover, the only way to mix them will be to distinguish both the UINT and the listing of user_age to UID, for example.
The C language can be very confusing, and sometimes we accept HOURS just to find out that a stupid error exists only because you used the wrong value as an argument, because the variables have similar names ... and the compiler obviously never will not complain because they are of the same type.
I did not find any attribute in the GCC manual, but I'm going to request it from the mailing list.
I just want to know HOW (and I know that no, I have a real question, WHY the standard and the compiler do not provide this, because I find this really useful and relatively one of the simplest things that need to be implemented on the compiler side), to specify some ATTRIBUTE for the type (as well as variables), to tell the compiler that TYPE should not be mixed with any other unless explicitly used. So, the real question is: - Why is this a bad idea? (Why doesn't the compiler consider this?) - 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 a program; The HUGE part of errors will be detected during the first compilation - passing arguments in the wrong order, using the wrong variable in a large and complex calculation ...
Sorry for my bad english.