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

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

问题描述

如果遇到了这种说法多个times 并不能找出什么是应该的意思。由于造成code是用一个常规的C编译器编译它最终会被类型检查,一样多(或很少),任何其他code。

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.

推荐答案

那么他们不是的直接的类型安全的...我在某些情况下假设/用法你可能会说他们可以是间接的(即产生code)类型安全的。但是,你当然可以创建适用于整数宏,并将其传递细绳...的pre处理器处理的宏肯定不会在意。编译器可能呛就可以了,根据使用...

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天全站免登陆