简体   繁体   English

#if 预处理器指令并不总是崩溃

[英]#if preprocessor directive doesn't always collapse

Say I define two #define preprocessor directives:假设我定义了两个 #define 预处理器指令:

#define TEST
#define TESTOFF

Now my code is organized in TEST and TESTOFF #if directives, eg:现在我的代码组织在 TEST 和 TESTOFF #if 指令中,例如:

#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.经常发生一个#if区域,比如#if TEST区域,变得可折叠而另一个( #if TESTOFF区域)不可折叠。

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预处理器指令的这种行为?

If the #If test is false, then obviously all of the code within (no matter what it's structure may be) is dead code.如果#If测试为假,那么显然其中的所有代码(无论其结构如何)都是死代码。 It makes sense to offer to collapse these sections.提供折叠这些部分是有意义的。

If the #If test is true, then arbitrary code may be contained within.如果#If测试为真,则其中可能包含任意代码。 So the collapse options are based on the code structure.所以折叠选项是基于代码结构的。 And no collapse is offered on the arbitrary #If test.并且在任意#If测试中不提供折叠。

Damien_The_Unbeliever's comment is correct. Damien_The_Unbeliever 的评论是正确的。 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. VS 为当前设置无效的部分(显示为灰色的部分)提供折叠功能,而不为活动部分提供折叠功能。 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.当调试配置处于活动状态时,底部将是可折叠的,但如果我将其切换到 Release,顶部将变得可折叠(而底部不可折叠)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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