如何在#ifdef 编译指令中为预处理器条件使用逻辑运算符?

How to use a logical operator for a preprocessor conditional in an #ifdef compilation directive?

我有以下使用预处理器的代码 conditional compilation directives:

#define foo
#define bar

#ifdef foo || !bar
  extern bool Verbose = FALSE;
#else
  extern bool Verbose = TRUE;
#endif

void start() {
}

由于以下错误,无法编译:

test.mq4(3,12) : error 175: '||' - expressions are not allowed on a global scope

然而,当第一行 (foo) 被注释掉时,代码编译正常,这似乎编译器在这种情况下允许在全局范围内使用此表达式(当 foo 未定义)。

您可以尝试使用 mql compilator 编译以上代码(在 Linux 下使用 wine):

mql.exe /mql4 test.mq4

所以问题是:

为什么这不起作用

我如何定义上述预处理器条件(foo || !bar )
正确的方法?


我也尝试过以下语法:

#if defined (foo) || defined (!bar)

user2357112 (GNU cpp syntax 之前所建议的那样),但它失败并出现以下错误:

error 109: '#if' - invalid preprocessor command

error 149: '#else' - unexpected token

因为MQL语法完全不同,不支持这种预处理器命令。

为什么这不起作用?

因为
条件 语法不符合 MQL4 预处理器.
的规范 ( 而是再次检查并交叉检查了 MQL4 Documentation
部分:
[
MQL4参考/语言基础/预处理器/条件编译(#ifdef,#ifndef,#else,#endif)] )


Preprocessor conditional compilation directives allow compiling or skipping a part of the program depending on the fulfillment of a certain condition.

That condition can take one of the following forms.

#ifdef identifier
// ... the code located here is compiled
// ... if identifier has already been defined for the preprocessor in #define directive.
#endif

#ifndef identifier
// ... the code located here is compiled
// ... if identifier is not currently defined by preprocessor #define directive.
#endif

A condition 是,在此 MQL4 预处理器上下文中,静态、封闭的公认句法指令构造函数集的成员 {#ifdef | #ifndef | #else}

一个 identifier 是,在这个 MQL4 预处理器上下文中,一个常量 term 不是 一个表达式.

Q.E.D.


正确的方法?

人们仍然可能会选择构建缺少的逻辑灵活性 "manually":

#ifdef bar_identifier
    #ifndef foo_indentifier
         extern bool aCompileTimePresetDefaultToExposedEXTERN_USER_INPUT = True;
    #else
         extern bool aCompileTimePresetDefaultToExposedEXTERN_USER_INPUT = False;
    #endif
#else
         extern bool aCompileTimePresetDefaultToExposedEXTERN_USER_INPUT = False;
#endif