具备可变数目的参数的宏-参数列的宏定义方法。

写给日志帮助宏,打日志常常须要格式化字符串,相似:DEBUG_STR(L"value = %d, error = %d.", val, error);html

so,定义方法以下:
oracle

来自:http://docs.oracle.com/cd/E19205-01/821-0389/bkacd/index.htmlide

C++ 编译器接受如下形式的 #define 预处理程序指令。spa


#define identifier (...) replacement_list
#define identifier (identifier_list, ...) replacement_list

若是列出的宏参数以省略号结尾,那么该宏的调用容许使用除了宏参数之外的其余更多参数。其余参数(包括逗号)收集到一个字符串中,宏替换列表中的名称 __VA_ARGS__ 能够引用该字符串。如下示例说明了如何使用可变参数列表的宏。debug


#define debug(...) fprintf(stderr, __VA_ARGS__)
#define showlist(...) puts(#__VA_ARGS__)
#define report(test, ...) ((test)?puts(#test):\
                        printf(__VA_ARGS__))
debug(“Flag”);
debug(“X = %d\n”,x);
showlist(The first, second, and third items.);
report(x>y, “x is %d but y is %d”, x, y);

其结果以下:日志


fprintf(stderr, “Flag”);
fprintf(stderr, “X = %d\n”, x);
puts(“The first, second, and third items.”);
((x>y)?puts(“x>y”):printf(“x is %d but y is %d”, x, y));
 

总得来说:htm

#define DEBUG_STR (...) logfunc(_VA_ARGS_)字符串

... 和 _VA_ARGS_get

相关文章
相关标签/搜索