宏观扩张和字符串化:如何获得宏名(而不是它的价值),使用另一个宏字符串化? [英] Macro expansion and stringification: How to get the macro name (not its value) stringified using another macro?
问题描述
出于兴趣:
#define _ACD 5, 5, 5, 30
#define DEFAULT_NETWORK_TOKEN_KEY_CLASS _ACD
#define DEFAULT_NETWORK_TOKEN_KEY { DEFAULT_NETWORK_TOKEN_KEY_CLASS }
使用DEFAULT_NETWORK_TOKEN_KEY_CLASS宏观而已,如何让_ACD在一个const无符号字符字符串化[]。
Using DEFAULT_NETWORK_TOKEN_KEY_CLASS macro only, how to get _ACD stringified in a const unsigned char [].
const uint8 startMsg[] = ?? DEFAULT_NETWORK_TOKEN_KEY_CLASS ;
将导致的 _ACD 仅
什么将是及彼_ACD正确的宏扩展。
在<上下文href=\"http://stackoverflow.com/questions/11689825/how-to-stringify-macro-having-array-as-define-a-macro-5-7-7-97\">How以字符串化宏有数组的#define a_macro {5,7,7,97}?
What will be the correct macro expansion for getting _ACD here. In context of How to stringify macro having array as #define a_macro {5,7,7,97}?
推荐答案
(标准免责声明关于没有一个很好的理由不滥用的C preprocessor适用于这里。)
(The standard disclaimer about not abusing the C preprocessor without a really good reason applies here.)
这当然有可能做你想做的事情。你需要一个字符串化
宏和宏观一点的间接
It's certainly possible to do what you want to do. You need a STRINGIFY
macro and a bit of macro indirection.
通常情况下,字符串化
与一个间接级别定义,机制,以便C preprocessor他们进行字符串化之前扩大它的参数。一个实现是:
Typically, STRINGIFY
is defined with one level of indirection, to allow the C preprocessor to expand its arguments before they undergo stringification. One implementation is:
/* The # operator converts symbol 'v' into a string */
#define STRINGIFY0(v) #v
#define STRINGIFY(v) STRINGIFY0(v)
不过,你会发现,这是不够的:
However, you'll find that this isn't enough:
#define _ACD 5, 5, 5, 30
#define DEFAULT_NETWORK_TOKEN_KEY_CLASS _ACD
#define DEFAULT_NETWORK_TOKEN_KEY { DEFAULT_NETWORK_TOKEN_KEY_CLASS }
#define START_MSG STRINGIFY(DEFAULT_NETWORK_TOKEN_KEY_CLASS)
const char startMsg[] = START_MSG;
在这里,字符串化(DEFAULT_NETWORK_TOKEN_KEY_CLASS)
扩展到 STRINGIFY0(5,5,5,30)
和çpreprocessor抱怨说,你给 STRINGIFY0
参数太多。
Here, STRINGIFY(DEFAULT_NETWORK_TOKEN_KEY_CLASS)
expands to STRINGIFY0(5,5,5,30)
, and the C preprocessor complains that you've given STRINGIFY0
too many arguments.
解决方案是延时 _ACD
的扩张,因此只有扩大到 5,5,5,30
当你想要它。要做到这一点,把它定义为一个函数宏:
The solution is to delay the expansion of _ACD
so it only expands to 5,5,5,30
when you want it to. To do this, define it as a function-like macro:
#define _ACD() 5, 5, 5, 30
这样, _ACD
将只当你呼它扩大: _ACD()
。 DEFAULT_NETWORK_TOKEN_KEY_CLASS
现在将扩大到 _ACD
,你必须按呼叫,它可以进一步展开: DEFAULT_NETWORK_TOKEN_KEY_CLASS()
。
This way, _ACD
will only be expanded when you "call" it: _ACD()
. DEFAULT_NETWORK_TOKEN_KEY_CLASS
will now expand to _ACD
, and you have to expand it further by "calling" it: DEFAULT_NETWORK_TOKEN_KEY_CLASS()
.
以下code说明了解决方案:
The following code illustrates the solution:
#include <stdio.h>
#define STRINGIFY0(v) #v
#define STRINGIFY(v) STRINGIFY0(v)
#define _ACD() 5, 5, 5, 30
#define DEFAULT_NETWORK_TOKEN_KEY_CLASS _ACD
#define DEFAULT_NETWORK_TOKEN_KEY { DEFAULT_NETWORK_TOKEN_KEY_CLASS() }
#define START_MSG STRINGIFY(DEFAULT_NETWORK_TOKEN_KEY_CLASS)
const char startMsg[] = START_MSG;
int main(int argc, char** argv)
{
printf("%s\n",startMsg);
return 0;
}
这篇关于宏观扩张和字符串化:如何获得宏名(而不是它的价值),使用另一个宏字符串化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!