C: create a type incompatible with any other types

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.

+5
source share
2 answers

Assuming the UINT is defined as some type, you can create typedefs for them to make them incompatible. The following code gives an idea of ​​how you initialize them and manipulate them.

 typedef struct { UINT id; } UID; typedef struct { UINT age; } AGE; UID user_id = { 1234 }; /* Example of initialization of a struct */ AGE user_age = { 23 }; int main() { UINT add; user_age.age = 25; /* example of assignment */ add = user_id + user_age; /* Example of compile error */ return 0; } 

There are some disadvantages. You cannot use structs with operators (+, -, /, *, etc.). i.e. You cannot add two structures together. Because of this, this will not work:

 AGE user1age = { 25 }; AGE user2age = { 23 }; UINT totage = user1age + user2age; 

You will need to do it this way and explicitly add age values ​​to the structs:

 AGE user1age = { 25 }; AGE user2age = { 23 }; UINT totage = user1age.age + user2age.age; 
+5
source

Use struct s:

 typedef struct { unsigned int uid; } UID; typedef struct { unsigned int age; } AGE; UID user_id; AGE user_age; 
+4
source

Source: https://habr.com/ru/post/1202600/


All Articles