可能重复: C预处理程序:在#define中使用#if吗?
在define的rhs中包含预处理器指令是否有任何技巧?问题是,预处理器将所有rhs折叠成一条长行。但是也许有一个窍门?
Is there any trick to have preprocessor directives inside rhs of define ? The problem is, preprocessor folds all rhs into one long line. But maybe there is a trick ?
在rhs中我想要的例子是
Example of what I'd want in the rhs is
#define MY_CHECK \ #ifndef MY_DEF \ # error MY_DEF not defined \ #endif? 目的是一个简短之处:使用1行快捷方式而不是多行检查顺序。
? The purpose is a shortness: to have 1-line shortcut instead of multiline sequence of checks.
推荐答案正如其他人指出的那样,预处理器宏不能扩展为任何其他预处理器指令。如果这样,您通常会在输入中误输入#字符。但是,有时您可以做一些事情来获得想要的东西。如果您想将宏扩展为以下内容:
As others have noted, preprocessor macros cannot expand into any other preprocessor directives; if they do you'll generally get odd errors about stray '#' characters in the input. However, sometimes there are things you can do to get what you want. If you want a macro that expands to something like:
#ifdef SOMETHING ...some code... #endif其中某些代码不包含任何预处理程序指令,可以定义一个IFDEF宏:
where some code doesn't include any preprocessor directives, you can define an IFDEF macro:
#ifdef SOMETHING #define IFDEF_SOMETHING(X) X #else #define IFDEF_SOMETHING(X) #endif然后在另一个宏中使用 IFDEF_SOMETHING(...一些代码...)。
and then use IFDEF_SOMETHING(...some code...) in your other macro.
如果您有一个一堆要重复多次的预处理器碎片,您可以将其粘贴到自己的文件中,然后在需要的每个位置使用 #include stuff 。
If you have a bunch of preprocessor cruft that you want to repeat multiple times, you can stick it in its own file and then use #include "stuff" in each spot you need it.
更多推荐
预处理指令内部定义?
发布评论