Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1489505
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: C/C++

2013-01-11 08:18:12


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

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

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

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

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

printf("%d*%d=%d\n", TOW, TOW, MUL(TOW,TOW)); 
这行的宏会被展开为: 
printf("%d*%d=%d\n", (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\n",  STR(INT_MAX));    // INT_MAX #i nclude 
这行会被展开为: 
printf("int max: %s\n", "INT_MAX"); 

printf("%s\n", CONS(A, A));               // compile error   
这一行则是: 
printf("%s\n", 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\n", STR(INT_MAX));          // INT_MAX,int型的最大值,为一个变量 #i nclude 
输出为: int max: 0x7fffffff 
STR(INT_MAX) -->  _STR(0x7fffffff) 然后再转换成字符串; 

printf("%d\n", 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];


#if _LFN_UNICODE /* Unicode string */
#if !_USE_LFN
#error _LFN_UNICODE must be 0 in non-LFN cfg.
#endif
#ifndef _INC_TCHAR
typedef WCHAR TCHAR;
#define _T(x) L ## x
#define _TEXT(x) L ## x
#endif

#else /* ANSI/OEM string */
#ifndef _INC_TCHAR
typedef char TCHAR;
#define _T(x) x
#define _TEXT(x) x
#endif

#endif
------------------------------------------------------

msgpack的例程代码:

点击(此处)折叠或打开

  1. #define msgpack_pack_inline_func(name) \
  2.     inline int msgpack_pack ## name
  3.     
  4. msgpack_pack_inline_func(_map)(msgpack_pack_user x, size_t n)
  5. {
  6.     if(n < 16) {
  7.         unsigned char d = 0x80 | (uint8_t)n;
  8.         msgpack_pack_append_buffer(x, &TAKE8_8(d), 1);
  9.     } else if(n < 65536) {
  10.         unsigned char buf[3];
  11.         buf[0] = 0xde; _msgpack_store16(&buf[1], (uint16_t)n);
  12.         msgpack_pack_append_buffer(x, buf, 3);
  13.     } else {
  14.         unsigned char buf[5];
  15.         buf[0] = 0xdf; _msgpack_store32(&buf[1], (uint32_t)n);
  16.         msgpack_pack_append_buffer(x, buf, 5);
  17.     }
  18. }


  19. #define msgpack_pack_inline_func_cint(name) \
  20.     inline int msgpack_pack ## name
  21.     
  22. msgpack_pack_inline_func_cint(_short)(msgpack_pack_user x, short d)
  23. {
  24. #if defined(SIZEOF_SHORT)
  25. #if SIZEOF_SHORT == 2
  26.     msgpack_pack_real_int16(x, d);
  27. #elif SIZEOF_SHORT == 4
  28.     msgpack_pack_real_int32(x, d);
  29. #else
  30.     msgpack_pack_real_int64(x, d);
  31. #endif

  32. #elif defined(SHRT_MAX)
  33. #if SHRT_MAX == 0x7fff
  34.     msgpack_pack_real_int16(x, d);
  35. #elif SHRT_MAX == 0x7fffffff
  36.     msgpack_pack_real_int32(x, d);
  37. #else
  38.     msgpack_pack_real_int64(x, d);
  39. #endif

  40. #else
  41. if(sizeof(short) == 2) {
  42.     msgpack_pack_real_int16(x, d);
  43. } else if(sizeof(short) == 4) {
  44.     msgpack_pack_real_int32(x, d);
  45. } else {
  46.     msgpack_pack_real_int64(x, d);
  47. }
  48. #endif
  49. }
  50.     

  51. #define msgpack_pack_inline_func_fixint(name) \
  52.     inline int msgpack_pack_fix ## name
  53.     
  54. msgpack_pack_inline_func_fixint(_uint8)(msgpack_pack_user x, uint8_t d)
  55. {
  56.     unsigned char buf[2] = {0xcc, TAKE8_8(d)};
  57.     msgpack_pack_append_buffer(x, buf, 2);
  58. }



阅读(1018) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~