测试是否在宏内定义了预处理器符号 [英] Test if preprocessor symbol is defined inside macro
问题描述
测试是否定义了预处理器符号的常用方法是使用#ifdef
.但是,不能在宏中使用#ifdef
.我需要的是一种方法来检查宏,如果该宏的参数是已定义的预处理器符号.
The usual way to test whether a preprocessor symbol is defined is using #ifdef
. However, #ifdef
cannot be used in a macro. What I need is a way to check in a macro if an argument of that macro is a defined preprocessor symbol.
例如:
#define TRACE(x,y) if(IS_DEFINED(x)){ std::cout << y; }
在这里,TRACE
带有两个参数,第一个x
应该是预处理器符号的名称.如果定义了此类符号,则应打印第二个参数.我正在寻找不存在的IS_DEFINED
函数/宏.
Here, TRACE
takes two arguments, the first x
should be the name of a preprocessor symbol. If such symbol is defined, the second argument should be printed. The non-existing IS_DEFINED
function/macro is what I am looking for.
用法如下:
#undef BLA
TRACE(BLA,"abc") // "abc" won't be printed, as BLA is not defined
#define BLA 1
TRACE(BLA,"xyz") // "xyz" will be printed, as BLA is a defined symbol
有没有办法实现这一目标?也许一些宏观魔术?当然,该解决方案应适用于任何符号,不仅适用于BLA
或一组硬编码符号.如果事先知道要检查的符号集,显然很容易.
Is there a way to achieve this? Maybe some macro magic? Of course, thes solution should work for any symbol, not only BLA
or a hardcoded set of symbols. It is obviously quite easy if the set of symbols to be checked is known in advance.