具備可變數目的參數的宏-參數列的宏定義方法。

寫給日誌幫助宏,打日誌常常須要格式化字符串,相似: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

相關文章
相關標籤/搜索