GCC预编译宏##,与另一个宏[重复]的标记连接

问题描述 投票:-1回答:1

正如你可以看到连接令牌的工作, 还有一个令牌是另一个宏工程, 但是当一个令牌是宏时,它似乎不起作用?

longNameForaFunction_one(){return 1;}
longNameForaFunction_two(){return 2;}
longNameForaFunction_third(){return 3;}
two(){return 2;}
#define bar two
#define foo(x)(longNameForaFunction_##x())
#define three third
main(){
printf("%d\n",foo(one)); // 1

printf("%d\n",foo(two)); // 2

printf("%d\n",bar()); // 2

// printf("%d\n",foo(three)); // this doesn't work  
}

如果取消注释,最后一行会出现此错误;

未定义的引用`longNameForaFunction_three'

#define three third

似乎没有效果

Try it online

c gcc macros c-preprocessor
1个回答
1
投票

那就是为什么你需要在它工作之前提供另一个级别 - 宏参数将在传递给foo之前进行扩展。

#define foo(x)(longNameForaFunction_##x())
#define foo1(x) foo(x)
#define three third

..

printf("%d\n",foo1(three));
© www.soinside.com 2019 - 2024. All rights reserved.