my-server
← Wiki Redirected from Variadic macro

Variadic macro in the C preprocessor

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.

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.

Additionally, regular macro arguments may be listed before the <code>...</code>, but regular arguments may not be listed after the <code>...</code>.

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 and C23 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.

The C preprocessor rules prevent macro names in the argument of 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, Clang (all versions), Visual Studio 2005, C++Builder 2006, and Oracle Solaris Studio (formerly Sun Studio) Forte Developer 6 update 2 (C++ version 5.3). GCC also supports such macros when compiling Objective-C.

Support for the 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 were desired, which would take the file and line number from which it was called as arguments, the following solution applies.

Our implemented function:

Due to limitations of the variadic macro support in C++11 the following straightforward solution can fail and should thus be avoided:

The reason is that

gets expanded to

where the comma before the closing brace will result in a syntax error.

GNU C++ supports a non-portable extension which solves this:

C++20 supports the following syntax.

By using the <code>cformat</code> string as part of the variadic arguments we can circumvent the abovementioned incompatibilities. This is tricky but portable.

could then be called as

which expands to

Another example is:

which expands to

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) will silently eliminate the trailing comma. Other compilers (e.g.: GCC) support putting in front of <code></code>.

The following application works

which expands to

which is equivalent to

But look at this application:

which expands to

which generates a syntax error with GCC.

GCC supports the following (non-portable) extension:

which removes the trailing comma when <code></code> is empty.

C23 solves this problem by introducing <code></code> like C++.

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:

could then be called as:

which expands to:

References

See also