预处理器变量的比较

问题描述 投票:0回答:1

这段代码无法编译的原因是什么?

#define THREADMODEL ASC 

#if THREADMODEL==NOASC
// This block should be ignored and the code should compile
#error
#endif

int main() {
}
c++ c-preprocessor preprocessor-directive
1个回答
13
投票

当预处理器解释时

#if THREADMODEL==NOASC

它将用

THREADMODEL
替换
ASC
:

#if ASC==NOASC

除非您有

#define
d
ASC
NOASC
具有数值,否则预处理器会将它们替换为 0 值(它采用任何未定义的符号并将其替换为 0):

#if 0==0

然后计算结果为

1
,因此预处理器将计算该块。

要解决此问题,请尝试为

ASC
NOASC
提供不同的数值,如下所示:

#define ASC    0
#define NOASC  (1 + (ASC))

希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.