请考虑以下代码:
#define TOTO 0xAA
#define TITI 0xBB
unsigned char val0xAA[] = {1, 2, 3};
unsigned char val0xBB[] = {4, 5, 6};
// Macro using Token pasting
#define VAL(_x_) val##_x_
void foo(void)
{
// Silly attempt to abuse preprocessor
unsigned char *tab1 = VAL(TOTO);
unsigned char *tab2 = VAL(TITI);
}
我希望VAL(TOTO)
宏扩展为val0xAA
,但实际上会扩展为valTOTO
并且编译失败。
问题是:在令牌粘贴评估之前,是否有可能“强制/优先”评估TOTO
?
答案 0 :(得分:3)
是的,这是一个常见的伎俩。只需添加另一个图层。
#define TOTO 0xAA
#define TITI 0xBB
unsigned char val0xAA[] = {1, 2, 3};
unsigned char val0xBB[] = {4, 5, 6};
// Macro using Token pasting
#define VAL1(_x_) val##_x_
#define VAL(_x_) VAL1(_x_) // expand and pass to VAL1 to concatenate
void foo(void)
{
// Silly attempt to abuse preprocessor
unsigned char *tab1 = VAL(TOTO);
unsigned char *tab2 = VAL(TITI);
}