2010-11-26 59 views
5

我需要编写一些代码来验证宏是否已定义但没有任何值。测试不需要编译时间。测试C宏的值是否为空

我试图写:

#if (funcprototype == "") 
MY_WARN("funcprototype is empty"); 
#endif 

代码不能编译,因为funcprototype扩展到空。

+0

该测试不需要编译时测试,但是您想在`#if`指令中使用结果?如果`funcprototype`有一个非空的替换列表,但在替换后它会扩展为空(由于重新扫描)。 – 2010-11-26 19:02:16

+0

这似乎是http://stackoverflow.com/questions/4102351/test-for-empty-macro-definition – 2010-11-26 19:04:21

回答

5

如果运行时检查是好的,那么你可以测试stringized更换长度:

#define REAL_STRINGIZE(x) #x 
#define STRINGIZE(x) REAL_STRINGIZE(x) 

if (STRINGIZE(funcprototype)[0] == '\0') { 
    // funcprototype expanded to an empty replacement list 
} 
else { 
    // funcprototype expanded to a non-empty replacement list 
} 

我不认为有一种普遍的情况“是这个宏通过替换为空令牌序列“编译时检查。这是一个类似的问题,“是否可以比较两个令牌的平等序列”,这在编译时是不可能的。