简体   繁体   English

如何注释掉多行宏中的代码块?

[英]How to comment out chunk of code in multi-line macro?

I have a complex multi line macro code in C++ (that process some geometry and computes its physical properties in RT) which is used many times and can not be converted to function (without huge space and performance hits). 我在C ++中有一个复杂的多行宏代码(用于处理某些几何图形并在RT中计算其物理属性),该代码已被多次使用并且无法转换为函数(没有巨大的空间和性能方面的优势)。 The problem is I sometimes need to configure the code inside disabling parts of code (in compile time for specific tasks/machines) something like this: 问题是我有时需要在禁用部分代码(在特定任务/机器的编译时)中配置代码,如下所示:

#define some_macro \
 bla;              \
 bla;              \
 if (0)            \
  {                \
  bla;             \
  bla;             \
  bla;             \
  }                \
 bla; 

As you can see this leads to multiples of unreachable code warnings due to macro repetitive usage. 如您所见,由于宏的重复使用,这导致许多unreachable code警告。 Using /* */ comments is possible but very bad for management of such code (at least for me) due to need of changing each line of code. 可以使用/* */注释,但由于需要更改每一行代码,因此对于管理此类代码(至少对我而言)非常不利。

The only other viable option I can think of is the use of macros something like: 我能想到的唯一可行的选择是使用宏,例如:

#define my_disable
#define some_macro  \
 bla;               \
 bla;               \
 #ifndef my_disable \
  bla;              \
  bla;              \
  bla;              \
 #endif             \
 bla; 

#undef my_disable

or using some volatile switch variable in the same manner: 或以相同的方式使用一些易失性开关变量:

volatile bool on=true;
#define some_macro \
 bla;              \
 bla;              \
 if (on)           \
  {                \
  bla;             \
  bla;             \
  bla;             \
  }                \
 bla; 

I rather not lose the multi line code formating as the macro is crucial and very complex, would be unreadable and unmanageable in time without it. 我宁愿不丢失多行代码的格式,因为宏至关重要且非常复杂,如果没有宏,它将无法及时读取和管理。 The same goes for separating this macro to more smaller ones. 将此宏分离为更小的宏也是如此。

Are there any other options not requiring changing all the lines of chunk of code or creating macro/variable on per chunk of code basis? 是否有其他选项不需要更改代码的所有行或在每个代码块的基础上创建宏/变量?

Another probably much easier solution would be to turn off the Warning for the part of code in question leaving the rest of code handling the warning as usual but I do not know the syntax. 另一个可能更简单的解决方案是针对有问题的代码部分关闭警告,而其余代码照常处理警告,但我不知道语法。 So alternative question would be: 因此,另一个问题是:

How to disable [C++ Warning] W8066 Unreachable code for just part of a source code? 如何仅对部分源代码禁用[C++ Warning] W8066 Unreachable code代码?

I am stuck with old BDS2006 C++ compiler so new language quirks would not help. 受困于旧的BDS2006 C ++编译器,因此新的语言怪癖无济于事。

How to disable [C++ Warning] W8066 Unreachable code for just part of a source code? 如何仅对部分源代码禁用[C++ Warning] W8066 Unreachable code代码?

You can wrap the code with #pragma warn statements: 您可以使用#pragma warn语句包装代码:

#pragma warn -8066 // disable W8066 warnings
<code>
#pragma warn .8066 // restore W8066 warnings back to default

Otherwise, I would suggest just making a copy of the macro, comment out the original, and modify the copy to remove the desired code as needed. 否则,我建议只复制一个宏,注释掉原始宏,然后修改该副本以根据需要删除所需的代码。

/*
#define some_macro \
 bla;              \
 bla;              \
 if (0)            \
  {                \
  bla;             \
  bla;             \
  bla;             \
  }                \
 bla; 
*/
#define some_macro \
 bla;              \
 bla;              \
 bla; 

Or, you could just comment out the desired code inside the original macro: 或者,您可以在原始宏中注释掉所需的代码:

#define some_macro \
 bla;              \
 bla;              \
 //if (0)            \
 // {                \
 // bla;             \
 // bla;             \
 // bla;             \
 // }                \
 bla; 

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

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