c - 为什么 typedef 不是强类型的?

标签 c typedef strong-typing

typedefs 不是强类型的原因是什么?是否有任何我看不到的好处,或者是由于向后兼容性?看这个例子:

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;
}

我不是在寻求解决方法来获得强类型数据类型,而只是想知道为什么标准不要求 typedefs 是强类型的?

谢谢。

最佳答案

因为 C 不是强类型的,typedef 的起源就是这种想法

typedef 只是为了方便和可读性,它不会创建新类型。

关于c - 为什么 typedef 不是强类型的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8182910/

相关文章:

c++ - 为什么我们可以取消引用函数指针?

boost - 根据 typedef 可以是 wcout 的通用 cout

c - typedef 有什么用,它的正确用途是什么?

typedef (timer_t) 的冲突类型错误

typescript - 在不同的模块中扩大 TypeScript 中的标记/区分联合?

c# - 字符串或其他密封类的强类型

C,管道消息到子进程不工作。为什么?

c - x86-32 上 C 中的堆栈溢出和段错误

c - ARM 指令到 C 语句

haskell - Haskell 中的强类型事件