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

Solution 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 --------|

Solution 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.