Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't typedefs strongly typed?

What's the reason for typedefs not being strongly typed? Is there any benefit I can't see or is it due 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 am not asking for workarounds to get a strong typed datatype but only want to know why the standard isn't requiring typedefs to be strongly typed?

Thank you.

like image 372
halex Avatar asked Nov 18 '11 13:11

halex


2 Answers

Because C is not strongly typed and typedef has its origin in that thinking

typedef is just for convenience and readability, it doesn't create a new type.

like image 64
AndersK Avatar answered Oct 16 '22 09:10

AndersK


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

C has in the contrary a whole idea of what compatible types are. This allows for example to link compilation units together, even if declarations of function protopyes are only done with compatible types and not with identical ones. All this comes from simple practical necessity in every day life, being still able to give some guarantees to implementations.

like image 21
Jens Gustedt Avatar answered Oct 16 '22 09:10

Jens Gustedt