Oracle® Solaris Studio 12.4: C++ ユーザーズガイド

印刷ビューの終了

更新: 2014 年 12 月
 
 

2.5.2 可変数の引数をとるマクロ

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));