#if defined(token) && token == ... - is this syntax legal?

c++ c-preprocessor

133 观看


1757 作者的声誉

assuming I have such code:

#if defined(SOMEDEF) && SOMEDEF >= 5
  // ...

Basically, SOMEDEF may not be defined but used in suppressed branch of operator &&. GCC accepts this code, but is it legal according to standard? Do all compilers support this?

作者: Andrei R. 的来源 发布者: 2017 年 9 月 15 日

回应 (3)


75394 作者的声誉

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:

#if SOMEDEF >= 5
  // ...
作者: Ken Thomases 发布者: 15.09.2017 05:40


2046 作者的声誉


Strictly, it depends.

#if defined(SOMEDEF) && SOMEDEF >= 5

Is legal if one of the following is true:

  • SOMEDEF is defined, and the macro expansion of SOMEDEF is such that the above is a valid expression.
  • SOMEDEF is not defined. In this case, SOMEDEF >= 5 is still a valid expression. Note that after macro replacement (and defined operator evaluation), any identifiers (except true/false) that are not defined are replaced with 0; 0 >= 5 is a valid subexpression.

For example, this is not valid:

#define SOMEDEF 0(0)
#if defined(SOMEDEF) && SOMEDEF >= 5

...because 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.

作者: H Walters 发布者: 15.09.2017 05:45


22 作者的声誉

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.

作者: Piyush Sonani 发布者: 15.09.2017 06:29