#if 预处理器指令可以嵌套在 C++ 中吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6678396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:31:01  来源:igfitidea点击:

Can #if pre-processor directives be nested in C++?

c++c-preprocessorpreprocessor-directive

提问by Armen Tsirunyan

I have a question about Pre-processor directives in c++:

我有一个关于 C++ 中预处理器指令的问题:

For example:

例如:

#ifndef QUESTION

//some code here

#ifndef QUESTION

//some code here

#endif

#endif

Can we use it in this way, and can the C++ compiler match the ifndefand endifin the right way?

我们可以这样使用它吗,C++ 编译器能否以正确的方式匹配ifndefendif

回答by Armen Tsirunyan

Yes, we can. The #endifstatement matches to the previous #if#ifdefor #ifndefetc for which there hasn't been a corresponding #endif.

我们可以。该#endif声明匹配到以前#if#ifdef#ifndef等还没有相应的针对#endif

e.g.

例如

#if  ----------|
#if  -----|    |
#endif ---|    |
#endif --------|

回答by Fred Foo

Yes, you can nest #if/#endifblocks. Some C coding styles would tell you to write

是的,您可以嵌套#if/#endif块。一些 C 编码风格会告诉你写

#ifdef CONDITION1
# ifdef CONDITION2
# endif
#endif

using spaces to denote the level of nesting.

使用空格来表示嵌套的级别。

回答by bert-jan

In your code, the #ifndef QUESTION section will be discarded unless you #undef QUESTION.

在您的代码中,#ifndef QUESTION 部分将被丢弃,除非您使用 #undef QUESTION。

Good luck!

祝你好运!