【发布时间】:2013-01-14 14:00:26
【问题描述】:
是否可以检查我的定义是否为空? IS_EMPTY_OR_UNDEFINED 是我刚刚想出的虚构宏。
#define constantA 0
#define constantB 1
#define constantC null
#define constantF ""
#define constantH
#if IS_EMPTY_OR_UNDEFINED(constantA)
# error constantA is defined 0 and the above if should not be true - this line should not run
#endif
#if IS_EMPTY_OR_UNDEFINED(constantB)
# error constantB is defined 1 and the above if should not be true - this line should not run
#endif
#if IS_EMPTY_OR_UNDEFINED(constantC)
# error constantC is defined null and the above if should not be true - this line should not run
#endif
#if IS_EMPTY_OR_UNDEFINED(constantF)
# error constantF is defined "" and the above if should not be true - this line should not run
#endif
#if ! IS_EMPTY_OR_UNDEFINED(constantH)
# error constantH is defined empty and the above if should not be true - this line should not run
#endif
#if defined(undefinedConstant) && ! IS_EMPTY_OR_UNDEFINED(undefinedConstant)
# error undefinedConstant is not defined and the above if should not be true - this line should not run
#endif
【问题讨论】:
-
你不能在预处理器中进行强制转换。预处理器只有两种类型,一种是有符号的,另一种是无符号整数常量。这些类型对应于编译器其他阶段已知的
intmax_t和uintmax_t。 -
它可以编译,我的所有测试都运行良好.. 很奇怪..
-
如果
constantA以+或-符号开头,您也许可以不用它,因为(int)实际上是预处理器的0。但是如果constantA是一个整数常量,则强制转换不会提供任何服务。那你为什么要这样做呢? -
好的。是的你是对的。它工作不正常。谢谢你的解释。
标签: objective-c c macros c-preprocessor