简体   繁体   中英

#if preprocessor directive doesn't always collapse

Say I define two #define preprocessor directives:

#define TEST
#define TESTOFF

Now my code is organized in TEST and TESTOFF #if directives, eg:

#if TEST
    ...
#endif

...MORE CODE...

#if TESTOFF
    ...
#endif

It often happens that one #if region, say the #if TEST region, becomes collapsible while the other ( #if TESTOFF region) is not.

Since this is a strange phenomenon that some might've never encountered, I'm appending a snapshot of the issue in question:交替可折叠 #if 区域

Does anyone know what parameters define this behavior behavior of the #if preprocessor directive?

If the #If test is false, then obviously all of the code within (no matter what it's structure may be) is dead code. It makes sense to offer to collapse these sections.

If the #If test is true, then arbitrary code may be contained within. So the collapse options are based on the code structure. And no collapse is offered on the arbitrary #If test.

Damien_The_Unbeliever's comment is correct. VS provides the collapse feature for sections that are inactive with your current settings (the parts that are shown in gray), and does not provide them for the active parts. So if I had this code:

#if DEBUG
     string a = "2";
     string b = "3";
#else
     string a = "3";
     string b = "3";
#endif

The bottom part would be collapsible while I have the Debug configuration active, but the top would become collapsible (and the bottom un-collapsible) if I switch it over to Release.

作为解决折叠问题和/或个人偏好的简单解决方法,值得注意的是,您可以将任何代码块括在大括号 {} 中,VS 将使其成为可折叠区域。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM