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

泄露秘密 提交于 2021-01-29 05:37:39

问题


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.

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?

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


回答1:


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

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; 


来源:https://stackoverflow.com/questions/56972172/how-to-comment-out-chunk-of-code-in-multi-line-macro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!