Variadic macro

Variadic macro

A variadic macro is a feature of the C preprocessor whereby a "macro" may be declared to accept a varying number of arguments.

Variable-argument macros were introduced in the ISO/IEC 9899:1999 (C99) revision of the C Programming Language standard in 1999. They are currently "not" part of the C++ programming language, though many popular C++ implementations support variable-argument macros as an extension, and it is expected that variadic macros may be added to C++ at a later date.

Declaration syntax

The declaration syntax is similar to that of variadic functions: an "ellipsis" "..." is used to indicate that zero or more arguments must be passed. During macro expansion each occurrence of the special identifier __VA_ARGS__ 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.

Support

The GNU Compiler Collection, since 3.0, C++Builder 2006 and Visual Studio 2005 [http://msdn2.microsoft.com/en-US/library/ms177415(VS.80).aspx] support variable-argument macros, both when compiling C and C++ code. In addition, GCC supports variadic macros when compiling Objective C. Sun Studio C and C++ have had support since Forte Developer 6 update 2 (C++ version 5.3) [Sun Studio feature comparison - http://developers.sun.com/sunstudio/support/CCcompare.html ] .

Example

If a printf-like function dprintf() were desired, which would take the file and line number from which it was called as arguments, the following macro might be used:void realdprintf (char const *file, int line, char const *fmt, ...);
#define dprintf(...) realdprintf(__FILE__, __LINE__, __VA_ARGS__)dprintf() could then be called as:dprintf("Hello, world");which expands to: realdprintf(__FILE__, __LINE__, "Hello, world"); or:dprintf("%d + %d = %d", 2, 2, 5);which expands to: realdprintf(__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.

References

ee also

* Variadic function


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Variadic function — In computer programming, a variadic function is a function of variable arity; that is, one which can take different numbers of arguments. Support for variadic functions differs widely among programming languages.There are many mathematical and… …   Wikipedia

  • Variadic — In computer science, a variadic operator or function is one that can take a varying number of arguments; that is, its arity is not fixed.For specific articles, see: * Variadic function * Variadic macro in the C preprocessor * Variadic templates… …   Wikipedia

  • C99 — is a modern dialect of the C programming language. History After the ANSI standardization process, the C language specification remained relatively static for some time, whereas C++ continued to evolve, largely during its own standardization… …   Wikipedia

  • C++0x — is the planned new standard for the C++ programming language. It is intended to replace the existing C++ standard, ISO/IEC 14882, which was published in 1998 and updated in 2003. These predecessors are informally known as C++98 and C++03. The new …   Wikipedia

  • C++11 — C++11, also formerly known as C++0x,[1] is the name of the most recent iteration of the C++ programming language, replacing C++TR1, approved by the ISO as of 12 August 2011.[2] The name is derived from the tradition of naming language versions by …   Wikipedia

  • Stdarg.h — is a header in the C standard library of the C programming language that allows functions to accept an indefinite number of arguments. C++ provides this functionality in the header ; the C header, though permitted, is deprecated in C++.The… …   Wikipedia

  • C preprocessor — The C preprocessor (cpp) is the preprocessor for the C and C++ computer programming languages. The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if). In many C… …   Wikipedia

  • Printf — The class of printf functions (which stands for print formatted ) is a class of functions, typically associated with curly bracket programming languages, that accept a string parameter (called the format string) which specifies a method for… …   Wikipedia

  • Perl 6 — Infobox programming language name = Perl paradigm = Multi paradigm year = 2000 designer = Larry Wall latest release version = pre release latest release date = typing = dynamic, static influenced by = Perl 5, Haskell, Smalltalk influenced =… …   Wikipedia

  • Lisp (programming language) — Infobox programming language name = Lisp paradigm = multi paradigm: functional, procedural, reflective generation = 3GL year = 1958 designer = John McCarthy developer = Steve Russell, Timothy P. Hart, and Mike Levin latest release version =… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”