Test if a C macro's value is empty

允我心安 提交于 2019-12-23 10:15:54

问题


I need to write some code to verify that a macro is defined but empty (not having any values). The test does not need to be in compile time.

I am attempting to write:

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

the code does not compile, as funcprototype expands to empty.


回答1:


If a run-time check is okay, then you can test the length of the stringized replacement:

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

I don't think there is a general-case "is this macro replaced by an empty sequence of tokens" compile-time check. That is a similar problem to "is it possible to compare two sequences of tokens for equality," which is impossible to do at compile-time.



来源:https://stackoverflow.com/questions/4287905/test-if-a-c-macros-value-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!