问题
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