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 (C99) revision of the C language standard, and in 2011 in ISO/IEC 14882:2011 (C++11) revision of the C++ language standard. Support for variadic macros with no arguments was added in C++20 and C23.
Both the C99 and C++11 standards require at least one argument, but since C++20 and C23 this limitation has been lifted through the <code>__VA_OPT__</code> functional macro. The <code>__VA_OPT__</code> macro is replaced by its argument when arguments are present, and omitted otherwise. Common compilers also permit passing zero arguments before this addition, however.
The C preprocessor rules prevent macro names in the argument of <code>__VA_OPT__</code> from expanding recursively. It is possible to work around this limitation up to an arbitrary fixed number of recursive expansions, however.
Support
Several compilers support variable-argument macros when compiling C and C++ code: the GNU Compiler Collection 3.0, Visual Studio 2005, GCC also supports such macros when compiling Objective-C.
Support for the <code>__VA_OPT__</code> macro to support zero arguments has been added in GNU Compiler Collection 8, Clang 6, and Visual Studio 2019.
Example
If a <code>printf</code>-like function <code>dbgprintf()</code> were desired, which would take the file and line number from which it was called as arguments, the following solution applies.
<!-- Don't change the example back to dprintf; it is a POSIX function: http://pubs.opengroup.org/onlinepubs/9699919799/functions/dprintf.html -->
Our implemented function:
<syntaxhighlight lang="c">
void realdbgprintf(const char* fileName, int line, const char* fmt, ...);
</syntaxhighlight>
Due to limitations of the variadic macro support in C++11 the following straightforward solution can fail and should thus be avoided:
<syntaxhighlight lang="cpp">
- define dbgprintf(cformat, ...) realdbgprintf(__FILE__, __LINE__, cformat, __VA_ARGS__)
</syntaxhighlight>
The reason is that
<syntaxhighlight lang="cpp">
dbgprintf("Hallo")
</syntaxhighlight>
gets expanded to
<syntaxhighlight lang="cpp">
realdbgprintf(__FILE__, __LINE__, "Hallo", )
</syntaxhighlight>
where the comma before the closing brace will result in a syntax error.
GNU C++ supports a non-portable extension which solves this:
<syntaxhighlight lang="c">
- define dbgprintf(cformat, ...) realdbgprintf(__FILE__, __LINE__, cformat, ##__VA_ARGS__)
</syntaxhighlight>
C++20 supports the following syntax.
<syntaxhighlight lang="cpp">
- define dbgprintf(cformat, ...) realdbgprintf(__FILE__, __LINE__, cformat __VA_OPT__(,) __VA_ARGS__)
</syntaxhighlight>
By using the <code>cformat</code> string as part of the variadic arguments we can circumvent the abovementioned incompatibilities. This is tricky but portable.
<syntaxhighlight lang="c">
- define dbgprintf(...) realdbgprintf(__FILE__, __LINE__, __VA_ARGS__)
</syntaxhighlight>
could then be called as
<syntaxhighlight lang="c">
dbgprintf("Hello, world");
</syntaxhighlight>
which expands to
<syntaxhighlight lang="c">
realdbgprintf(__FILE__, __LINE__, "Hello, world");
</syntaxhighlight>
Another example is:
<syntaxhighlight lang="c">
dbgprintf("%d + %d = %d", 2, 2, 5);
</syntaxhighlight>
which expands to
<syntaxhighlight lang="c">
realdbgprintf(__FILE__, __LINE__, "%d + %d = %d", 2, 2, 5);
</syntaxhighlight>
Without variadic macros, writing wrappers to <code>printf</code> is not directly possible. The standard workaround is to use the stdargs functionality of C/C++, and have the function call <code>vprintf</code> instead.
Trailing comma
There is a portability issue with generating a trailing comma with empty args for variadic macros in C99. Some compilers (e.g., Visual Studio when not using the new standard-conformant preprocessor
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 <code>printf()</code> function:
<syntaxhighlight lang="c">
- define dbgprintf(x) realdbgprintf x
</syntaxhighlight>
could then be called as:
<syntaxhighlight lang="c">
dbgprintf(("Hello, world %d", 27));
</syntaxhighlight>
which expands to:
<syntaxhighlight lang="c">
realdbgprintf("Hello, world %d", 27);
</syntaxhighlight>
Rust
In Rust, a variadic interface (also known as a variadic macro), allows for taking an arbitrary number of arguments. Rust lacks variadic functions and variadic templates, thus the only way variadic parameters can be accomplished is through said variadic interfaces, which use repetition patterns through <code>macro_rules!</code>. For example, in <syntaxhighlight lang="rust" inline>$( ... ),*</syntaxhighlight>, <code>$()</code> denotes a repeating pattern, <code>,</code> denotes a separator, <code>*</code> denotes zero or more repetitions (a Kleene star, while <code>+</code> which matches one or more would be a Kleene plus).
One such example of implementing a variadic macro is the following, equivalent to <code>vec!</code>:
<syntaxhighlight lang="rust">
// Define a macro named "make_vec!"
macro_rules! make_vec {
// Matches any Rust expression
// and repeat the pattern separated by commas, zero or more times
( $( $x:expr ),* ) => {
{
// Vec must be fully qualified
// to prevent resolving to a user-defined Vec
let mut temp = ::std::vec::Vec::new();
// Repeat a push to temp once per argument
$(
temp.push($x);
)*
// Return temp from the macro
temp
}
};
}
</syntaxhighlight>
References
See also
- Variadic function
- Variadic template
ja:可変長引数
