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

孤者浪人 提交于 2019-12-11 04:28:10

问题


I have a question about Pre-processor directives in 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 ifndef and endif in the right way?


回答1:


Yes, we can. The #endif statement matches to the previous #if #ifdef or #ifndef etc for which there hasn't been a corresponding #endif.

e.g.

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



回答2:


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

#ifdef CONDITION1
# ifdef CONDITION2
# endif
#endif

using spaces to denote the level of nesting.




回答3:


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

Good luck!



来源:https://stackoverflow.com/questions/38963852/nested-else-handling-in-false-ifdef-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!