Why not typed typedefs?

What reason for typedefs not strongly typed? Is there any benefit that I don't see or is it related to backward compatibility? See this example:

 typedef int Velocity; void foo(Velocity v) { //do anything; } int main() { int i=4; foo(i); //Should result in compile error if strongly typed. return 0; } 

I do not ask for workarounds to get a strict data type, but just want to know why the standard does not require typedefs be strongly typed?

Thanks.

+6
source share
3 answers

Since C is not strongly typed, and typedef has its origin in thinking

typedef is for convenience and readability only; it does not create a new type.

+14
source

typedef is just a missnomer (like many other keywords). Think of it as typealias .

C has the opposite view of compatible types. This allows, for example, linking compilation units together, even if declarations of function prototypes are executed only with compatible types, and not with identical ones. All this comes from a simple practical necessity in everyday life, still able to give some guarantees for implementation.

+4
source

Even if Velocity was a different type from int , your code will compile and work just fine due to type conversion rules. Whatever works is to pass an expression of type Velocity * function that expects int * , etc. If you want to achieve the last form of forced type input, just make a Velocity structure or union type containing a single integer, and now you will have a new real type.

0
source

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


All Articles