为什么C宏不是类型安全? [英] Why are C macros not type-safe?

查看:132
本文介绍了为什么C宏不是类型安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果遇到此声明,请多个 ,无法弄清楚这是什么意思。由于生成的代码是使用常规的C编译器编译的,所以它最终只是类型检查一样多(或少)任何其他代码。

If have encountered this claim multiple times and can't figure out what it is supposed to mean. Since the resulting code is compiled using a regular C compiler it will end up being type checked just as much (or little) as any other code.

那么为什么宏不是类型安全?

So why are macros not type safe? It seems to be one of the major reasons why they should be considered evil.

推荐答案

它们不是直接的类型安全...我想在某些情况/用法,你可以认为他们可以间接(即结果代码)类型安全。但你可以创建一个宏为整数和传递它的字符串...预处理器处理宏肯定不在乎。编译器可能会阻塞,取决于用法...

Well they're not directly type-safe... I suppose in certain scenarios/usages you could argue they can be indirectly (i.e. resulting code) type-safe. But you could certainly create a macro intended for integers and pass it strings... the pre-processor handling the macros certainly doesn't care. The compiler may choke on it, depending on usage...

这篇关于为什么C宏不是类型安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆