#if defined(token) && token == ... - is this syntax legal?
Yes, the code you have in your question is valid. In fact, you don't even need to check if
SOMEDEF is defined. It will be assumed to be 0 if it isn't. So, this is functionally equivalent:
作者: Ken Thomases 发布者: 15.09.2017 05:40
#if SOMEDEF >= 5 // ... #endif
Strictly, it depends.
#if defined(SOMEDEF) && SOMEDEF >= 5
Is legal if one of the following is true:
SOMEDEFis defined, and the macro expansion of
SOMEDEFis such that the above is a valid expression.
SOMEDEFis not defined. In this case,
SOMEDEF >= 5is still a valid expression. Note that after macro replacement (and
definedoperator evaluation), any identifiers (except
false) that are not defined are replaced with
0 >= 5is a valid subexpression.
For example, this is not valid:
#define SOMEDEF 0(0) #if defined(SOMEDEF) && SOMEDEF >= 5
0(0) >= 5 is not a valid subexpression.
for instance, `#if defined(__has_cpp_attribute) && __has_cpp_attribute(deprecated)` doesn't work
You mean to say, this doesn't work when
__has_cpp_attribute is not defined. The same thing happens in this case;
0(0) is not a valid subexpression.
Yes, this syntax is totally valid. Here if
SOMEDEF defined than only
SOMEDEF >= 5 will going to be expanded else block will be not expanded.