Chinaunix首页 | 论坛 | 博客
  • 博客访问: 67027
  • 博文数量: 18
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 160
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-20 19:05
文章分类
文章存档

2016年(3)

2015年(15)

我的朋友

分类: C/C++

2016-09-01 16:57:10

在宏中使用##会导致后面的宏参数不能展开,比如以下情况

点击(此处)折叠或打开

  1. #define TESNUM 10

  2. #define FUNCNAME(funcname,index) funcname ##_ ##index

  3. int API_CLIENT_FUNCNAME(testname,TESNUM)(int x)
  4. {
  5.     printf("num = %d,test!\n",x);
  6.     return 0;
  7. }
    0000000000000029 T main
                     U printf
    0000000000000000 T testname_TESNUM
    TESNUM不能替换为10.



解决办法,多加一层宏定义:

点击(此处)折叠或打开

  1. #define TESNUM 10

  2. #define FUNCNAME(funcname,index) funcname ##_ ##index
  3. #define FUNCNAME2(funcname,index) FUNCNAME(funcname,index)

  4. int API_CLIENT_FUNCNAME2(testname,TESNUM)(int x)
  5. {
  6.     printf("num = %d,test!\n",x);
  7.     return 0;
  8. }
    0000000000000029 T main
                     U printf
    0000000000000000 T testname_10

Sources from gcc.gnu.org

3.4 Stringification

Sometimes you may want to convert a macro argument into a string constant. Parameters are not replaced inside string constants, but you can use the ‘#’ preprocessing operator instead. When a macro parameter is used with a leading ‘#’, the preprocessor replaces it with the literal text of the actual argument, converted to a string constant. Unlike normal parameter replacement, the argument is not macro-expanded first. This is called stringification.

3.5 Concatenation

Both the tokens combined by ‘##’ could come from the macro body, but you could just as well write them as one token in the first place. Token pasting is most useful when one or both of the tokens comes from a macro argument. If either of the tokens next to an ‘##’ is a parameter name, it is replaced by its actual argument before ‘##’ executes. As with stringification, the actual argument is not macro-expanded first. If the argument is empty, that ‘##’ has no effect.



阅读(1642) | 评论(0) | 转发(0) |
0

上一篇:select函数第一个参数的理解,为什么+1

下一篇:没有了

给主人留下些什么吧!~~