写给日志帮助宏,打日志经常需要格式化字符串,类似:DEBUG_STR(L"value = %d, error = %d.", val, error);
so,定义方法如下:
来自:http://docs.oracle.com/cd/E19205-01/821-0389/bkacd/index.html
C++ 编译器接受以下形式的 #define 预处理程序指令。
#define identifier (...) replacement_list #define identifier (identifier_list, ...) replacement_list |
如果列出的宏参数以省略号结尾,那么该宏的调用允许使用除了宏参数以外的其他更多参数。其他参数(包括逗号)收集到一个字符串中,宏替换列表中的名称 __VA_ARGS__ 可以引用该字符串。以下示例说明了如何使用可变参数列表的宏。
#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)); |
总得来讲:
#define DEBUG_STR (...) logfunc(_VA_ARGS_)
... 和 _VA_ARGS_
原文链接: https://www.cnblogs.com/lebronjames/p/3193508.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/96072
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!