The C ++ compiler accepts# DefinePreprocessing Program commands.
#define identifier (...) replacement_list#define identifier (identifier_list, ...) replacement_list |
If the listed macro parameters end with a ellipsis, more macro parameters except macro parameters can be called. Other parameters (including commas) are collected into a string, and the macro replaces the name in the list._ VA_ARGS __This string can be referenced. The following example shows how to use the macro of the variable parameter list.
#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); |
The result is as follows:
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)); |