A variadic macro is a feature of some computer programming languages, especially the C preprocessor, whereby a macro may be declared to accept a varying number of arguments. Variable-argument macros were introduced in 1999 in the ISO/IEC 9899:1999 revision of the C language standard, and in 2011 in ISO/IEC 14882:2011 revision of the C++ language standard. Support for variadic macros with no arguments was added in C++20.
Declaration syntax
The declaration syntax is similar to that of variadic functions: a sequence of three full stops "" is used to indicate that one or more arguments must be passed. During macro expansion each occurrence of the special identifier in the macro replacement list is replaced by the passed arguments. No means is provided to access individual arguments in the variable argument list, nor to find out how many were passed. However, macros can be written to count the number of arguments that have been passed. Both the C99 and C++11 standards require at least one argument, but since C++20 this limitation has been lifted through the functional macro. The macro is replaced by its argument when arguments are present, and omitted otherwise. Common compilers also permit passing zero arguments before this addition, however.
If a printf-like function were desired, which would take the file and line number from which it was called as arguments, the following solution applies. // Our implemented function void realdbgprintf ; // Due to limitations of the variadic macro support in C++11 the following // straightforward solution can fail and should thus be avoided: // // #define dbgprintf \ // realdbgprintf // // The reason is that // // dbgprintf // // gets expanded to // // realdbgprintf // // where the comma before the closing brace will result in a syntax error. // // GNU C++ supports a non-portable extension which solves this. // // #define dbgprintf \ // realdbgprintf // // C++20 eventually supports the following syntax. // // #define dbgprintf \ // realdbgprintf // // By using the 'cformat' string as part of the variadic arguments we can // circumvent the abovementioned incompatibilities. This is tricky but // portable.
define dbgprintf realdbgprintf
could then be called as dbgprintf ;
which expands to realdbgprintf ;
Another example is dbgprintf;
which expands to realdbgprintf;
Without variadic macros, writing wrappers to printf is not directly possible. The standard workaround is to use the stdargs functionality of C/C++, and have the function callvprintf instead.
Trailing comma
There is a portability issue with generating a trailing comma with empty args for variadic macros in C99. Some compilers will silently eliminate the trailing comma. Other compilers support putting in front of.
define MYLOG fprintf
The following application works MYLOG;
which expands to fprintf ;
which is equivalent to fprintf ;
But look at this application: MYLOG;
which expands to fprintf ;
which generates a syntax error with GCC. GCC supports the following extension:
define MYLOG fprintf
which removes the trailing comma when is empty.
Alternatives
Before the existence of variable-arguments in C99, it was quite common to use doubly nested parentheses to exploit the variable number of arguments that could be supplied to the function: