C++ 多行#if

C++ MultiLine #if

我已经尝试 google 这一段时间了,但我似乎无法找到任何明确的答案,如果可以的话。

我想知道是否可以在 C++ 中以类似于这种类型的 if

的方式执行多行 #if 语句
if (
   1 == 1 ||
   2 == 2 ||
   3 == 3
) {
   cout << "True\n";
}

我希望得到类似的东西(我知道这是无可救药的错误)

#if
   1 == 1 ||
   2 == 2 ||
   3 == 3
#then
   cout << "True\n";
#else
   cout << "False\n";
#endif

是的。续行:

#if \
   1 == 1 || \
   2 == 2 || \
   3 == 3
   cout << "True\n";
#else
   cout << "False\n";
#endif
#if \
   1 == 1 || \
   2 == 2 || \
   3 == 3
   cout << "True\n";
#else
   cout << "False\n";
#endif

Backslash-newline 组合在预处理的早期就被剥离,甚至在对输入进行分词之前。您可以使用它在多条物理线路上传播预处理器指令。

哎呀,理论上你甚至可以做到

#i\
f 1 == 1 |\
| 2 == 2 || 3 =\
= 3

但是你的同事可能会生你的气。

正确的使用方法如下。每个预处理宏都可以在当前行的末尾使用 \ 写成多行。 cpp中没有#then关键字:)

    #if \
       1 == 1 || \
       2 == 2 || \
       3 == 3
       cout << "True\n";
    #else
       cout << "False\n";
    #endif

我认为这个问题的答案是使用字符转义作为行尾 '\'

 #if \
   1 == 1 || \
   2 == 2 || \
   3 == 3
   void foo(){}
#else
   void bar(){}
#endif