如何注释掉多行宏中的代码块? [英] How to comment out chunk of code in multi-line macro?

查看:81
本文介绍了如何注释掉多行宏中的代码块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中有一个复杂的多行宏代码(可处理某些几何图形并在RT中计算其物理属性),该代码已被多次使用并且无法转换为函数(没有巨大的空间和性能优势).问题是我有时需要在禁用部分代码(在针对特定任务/机器的编译时)中配置代码,如下所示:

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

是否还有其他选项不需要更改所有代码行或在每个代码块的基础上创建宏/变量?

另一种可能更容易的解决方案是关闭有问题的代码部分的警告,而其余代码照常处理警告,但我不知道语法.因此,另一个问题是:

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:

如何仅对部分源代码禁用 [C ++警告] W8066无法访问的代码?

How to disable [C++ Warning] W8066 Unreachable code for just part of a source code?

我受困于旧的 BDS2006 C ++ 编译器,因此新的语言怪癖无济于事.

I am stuck with old BDS2006 C++ compiler so new language quirks would not help.

推荐答案

如何仅对部分源代码禁用 [C ++警告] W8066无法访问的代码?

您可以使用 #pragma warn 语句包装代码:

You can wrap the code with #pragma warn statements:

#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; 

这篇关于如何注释掉多行宏中的代码块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆