Variadic macro
≈ 4 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
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.
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. void debugPrint(const char* file, int line, const char* fmt, ...); Due to limitations of the variadic macro support in C++11 the following straightforward solution can fail and should thus be avoided: define dbgprintf(cformat, ...) debugPrint(__FILE__, __LINE__, cformat, __VA_ARGS__) The reason is that dbgprintf("Hallo") gets expanded to debugPrint(__FILE__, __LINE__, "Hallo", ) 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(cformat, ...) debugPrint(__FILE__, __LINE__, cformat, ##__VA_ARGS__) C++20 supports the following syntax. define dbgprintf(cformat, ...) debugPrint(__FILE__, __LINE__, cformat __VA_OPT__(,) __VA_ARGS__) By using the cformat string as part of the variadic arguments we can circumvent the abovementioned incompatibilities. This is tricky but portable. define dbgprintf(...) debugPrint(__FILE__, __LINE__, __VA_ARGS__) could then be called as dbgprintf("Hello, world"); which expands to debugPrint(__FILE__, __LINE__, "Hello, world"); Another example is: dbgprintf("%d + %d = %d", 2, 2, 5); which expands to debugPrint(__FILE__, __LINE__, "%d + %d = %d", 2, 2, 5); 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 call vprintf instead. define MYLOG(FormatLiteral, ...) fprintf(stderr, "%s(%u): " FormatLiteral "\n", __FILE__, __LINE__, __VA_ARGS__) The following application works MYLOG("Too many balloons %u", 42); which expands to fprintf(stderr, "%s(%u): " "Too many balloons %u" "\n", __FILE__, __LINE__, 42); which is equivalent to fprintf(stderr, "%s(%u): Too many balloons %u\n", __FILE__, __LINE__, 42); But look at this application: MYLOG("Attention!"); which expands to fprintf(stderr, "%s(%u): " "Attention!" "\n", __FILE__, __LINE__, ); which generates a syntax error with GCC. GCC supports the following (non-portable) extension: define MYLOG(FormatLiteral, ...) fprintf(stderr, "%s(%u): " FormatLiteral "\n", __FILE__, __LINE__, ##__VA_ARGS__) which removes the trailing comma when __VA_ARGS__ is empty. C23 solves this problem by introducing __VA_OPT__ like C++. define dbgprintf(x) debugPrint x could then be called as: dbgprintf(("Hello, world %d", 27)); which expands to: debugPrint("Hello, world %d", 27); 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 macro_rules! . For example, in $( ... ),* , $() denotes a repeating pattern, , denotes a separator, * denotes zero or more repetitions (a Kleene star, while + which matches one or more would be a Kleene plus). One such example of implementing a variadic macro is the following, equivalent to vec! : // 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 } }; }
Text derived from https://en.wikipedia.org/w/index.php?oldid=1362239917 under CC BY-SA 4.0; modifications have been made.