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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With