宏#/##

原文: https://blog.csdn.net/ithomer/article/details/6004159

一、一般用法  
我们使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起.  
用法:  
#include<cstdio>  
#include<climits>  
using namespace std;  

#define STR(s) #s  
#define CONS(a,b) int(a##e##b)  

int main()  
{  
  printf(STR(vck)); // 输出字符串"vck"  
  printf("%d  
", CONS(2,3)); // 2e3 输出:2000  
  return 0;  

__VA_ARGS__:总体来说就是将左边宏中 ... 的内容原样抄写在右边 __VA_ARGS__ 所在的位置。它是一个可变参数的宏,是新的C99规范中新增的,目前似乎只有gcc支持
    #ifndef LOG_NDEBUG_FUNCTION  
    #define LOGFUNC(...) ((void)0)  
    #else  
    #define LOGFUNC(...) (printk(__VA_ARGS__))  
    #endif  


   #define LOGFUNC(fmt, ...) printf(fmt,##__VA_ARGS__)  //在可变参数...为空时, ##可以消除前面的,号


二、当宏参数是另一个宏的时候  
需要注意的是凡宏定义里有用'#'或'##'的地方宏参数是不会再展开.  

1, 非'#'和'##'的情况  
#define TOW (2)  
#define MUL(a,b) (a*b)  

printf("%d*%d=%d  
", TOW, TOW, MUL(TOW,TOW));  
这行的宏会被展开为:  
printf("%d*%d=%d  
", (2), (2), ((2)*(2)));  
MUL里的参数TOW会被展开为(2).  

2, 当有'#'或'##'的时候  
#define A (2)  
#define STR(s) #s  
#define CONS(a,b) int(a##e##b)  

printf("int max: %s  
", STR(INT_MAX)); // INT_MAX #include<climits>  
这行会被展开为:  
printf("int max: %s  
", "INT_MAX");  

printf("%s  
", CONS(A, A)); // compile error   
这一行则是:  
printf("%s  
", int(AeA));  

INT_MAX和A都不会再被展开, 然而解决这个问题的方法很简单. 加多一层中间转换宏.  
加这层宏的用意是把所有宏的参数在这层里全部展开, 那么在转换宏里的那一个宏(_STR)就能得到正确的宏参数.  

#define A (2)  
#define _STR(s) #s  
#define STR(s) _STR(s) // 转换宏  
#define _CONS(a,b) int(a##e##b)  
#define CONS(a,b) _CONS(a,b) // 转换宏  

printf("int max: %s  
", STR(INT_MAX)); // INT_MAX,int型的最大值,为一个变量 #include<climits>  
输出为: int max: 0x7fffffff  
STR(INT_MAX) --> _STR(0x7fffffff) 然后再转换成字符串;  

printf("%d  
", CONS(A, A));  
输出为:200  
CONS(A, A) --> _CONS((2), (2)) --> int((2)e(2))  

三、'#'和'##'的一些应用特例  
1、合并匿名变量名  
#define ___ANONYMOUS1(type, var, line) type var##line  
#define __ANONYMOUS0(type, line) ___ANONYMOUS1(type, _anonymous, line)  
#define ANONYMOUS(type) __ANONYMOUS0(type, __LINE__)  
例:ANONYMOUS(static int); 即: static int _anonymous70; 70表示该行行号;  
第一层:ANONYMOUS(static int); --> __ANONYMOUS0(static int, __LINE__);  
第二层: --> ___ANONYMOUS1(static int, _anonymous, 70);  
第三层: --> static int _anonymous70;  
即每次只能解开当前层的宏,所以__LINE__在第二层才能被解开;  

2、填充结构  
#define FILL(a) {a, #a}  

enum IDD{OPEN, CLOSE};  
typedef struct MSG{  
  IDD id;  
  const char * msg;  
}MSG;  

MSG _msg[] = {FILL(OPEN), FILL(CLOSE)};  
相当于:  
MSG _msg[] = {{OPEN, "OPEN"},  
  {CLOSE, "CLOSE"}};  

3、记录文件名  
#define _GET_FILE_NAME(f) #f  
#define GET_FILE_NAME(f) _GET_FILE_NAME(f)  
static char FILE_NAME[] = GET_FILE_NAME(__FILE__);  

4、得到一个数值类型所对应的字符串缓冲大小  
#define _TYPE_BUF_SIZE(type) sizeof #type  
#define TYPE_BUF_SIZE(type) _TYPE_BUF_SIZE(type)  
char buf[TYPE_BUF_SIZE(INT_MAX)];  
  --> char buf[_TYPE_BUF_SIZE(0x7fffffff)];  
  --> char buf[sizeof "0x7fffffff"];  
这里相当于:  
char buf[11];

===========================

Source:

  1. /*********************************** 
  2.  * 
  3.  * #  把宏参数变为一个字符串 
  4.  * ## 把两个宏参数贴合在一起 
  5.  * Author: sunboy_2050 
  6.  * Date  : 2010-11-22 
  7.  * 
  8.  **********************************/  
  9. #include <iostream>  
  10. #include <climits>   
  11. using namespace std;  
  12. #define STR(s) #s  
  13. #define CONS(a, b) int(a##e##b)  
  14. #define TOW (2)  
  15. #define MUL(a, b) (a*b)  
  16. #define A 2  
  17. #define _STR(s) #s  
  18. #define STR2(s) _STR(s) //转换宏  
  19. #define _CONS(a, b) int(a##e##b)  
  20. #define CONS2(a, b) _CONS(a, b) //转换宏  
  21. #define __ANONYMOUS1(type, var, line) type var##line  
  22. #define _ANONYMOUS0(type, line) __ANONYMOUS1(type, _anonymous, line)  
  23. #define ANONYMOUS(type) _ANONYMOUS0(type, __LINE__)  
  24. #define FILL(a) {a, #a}  
  25. #define _TYPE_BUF_SIZE(type) sizeof #type  
  26. #define TYPE_BUF_SIZE(type) _TYPE_BUF_SIZE(type)  
  27.   
  28. int main()  
  29. {  
  30.     cout<<(STR(abcd))<<endl; //输出 "abcd"  
  31.     cout<<(STR("abcd"))<<endl; //输出 ""abcd""  
  32.     cout<<CONS(2, 3)<<endl;  //输出 2e3 = 2*10^3 = 2000  
  33.     cout<<endl;  
  34.     cout<<TOW<<"*"<<TOW<<"="<<MUL(TOW, TOW)<<endl; //(2)*(2)=((2)*(2))=4  
  35.       
  36.     cout<<endl;  
  37.     cout<<"STR2(INT_MAX): "<<STR2(INT_MAX)<<endl; //STR2(INT_MAX) --> _STR(0x7fffffff) --> _STR(2147483647)   
  38.     cout<<"CONS2(A, A): "<<CONS2(A, A)<<endl; //CONS2(A, A) --> _CONS(2, 2) --> int(2e2) --> 2*10^2 --> 200  
  39.       
  40.     cout<<endl;  
  41.     //cout<<ANONYMOUS(static int)<<endl;  
  42.     cout<<endl;  
  43.     enum IDD{OPEN, CLOSE};  
  44.     typedef struct _MSG{  
  45.         IDD id;  
  46.         const char *msg;  
  47.     }MSG;  
  48.     MSG _msg[] = {FILL(OPEN), FILL(CLOSE)}; //{FILL(OPEN), FILL(CLOSE)} --> {{OPEN, "OPEN"},{CLOSE, "CLOSE"}}  
  49.     cout<<_msg<<endl;  
  50.     cout<<endl;  
  51.     char buf[TYPE_BUF_SIZE(INT_MAX)];  
  52.     cout<<TYPE_BUF_SIZE(INT_MAX)<<endl;  
  53.       
  54.     return 0;  
  55. }  
 

Result:

[[email protected] c++]$ g++ -o define1 define1.c 

[[email protected] c++]$ ./define1                

abcd

"abcd"

2000

2*2=4

STR2(INT_MAX): 2147483647

CONS2(A, A): 200

0x7fbffff750

11

猜你喜欢

转载自blog.csdn.net/twd_1991/article/details/80689871
今日推荐