]>
Commit | Line | Data |
---|---|---|
c63539ff ML |
1 | .. |
2 | Copyright 1988-2022 Free Software Foundation, Inc. | |
3 | This is part of the GCC manual. | |
4 | For copying conditions, see the copyright.rst file. | |
5 | ||
6 | .. index:: variable number of arguments, macros with variable arguments, variadic macros | |
7 | ||
8 | .. _variadic-macros: | |
9 | ||
10 | Variadic Macros | |
11 | *************** | |
12 | ||
13 | A macro can be declared to accept a variable number of arguments much as | |
14 | a function can. The syntax for defining the macro is similar to that of | |
15 | a function. Here is an example: | |
16 | ||
17 | .. code-block:: c++ | |
18 | ||
19 | #define eprintf(...) fprintf (stderr, __VA_ARGS__) | |
20 | ||
21 | This kind of macro is called :dfn:`variadic`. When the macro is invoked, | |
22 | all the tokens in its argument list after the last named argument (this | |
23 | macro has none), including any commas, become the :dfn:`variable | |
24 | argument`. This sequence of tokens replaces the identifier | |
25 | ``__VA_ARGS__`` in the macro body wherever it appears. Thus, we | |
26 | have this expansion: | |
27 | ||
28 | .. code-block:: | |
29 | ||
30 | eprintf ("%s:%d: ", input_file, lineno) | |
31 | → fprintf (stderr, "%s:%d: ", input_file, lineno) | |
32 | ||
33 | The variable argument is completely macro-expanded before it is inserted | |
34 | into the macro expansion, just like an ordinary argument. You may use | |
35 | the :samp:`#` and :samp:`##` operators to stringize the variable argument | |
36 | or to paste its leading or trailing token with another token. (But see | |
37 | below for an important special case for :samp:`##`.) | |
38 | ||
39 | If your macro is complicated, you may want a more descriptive name for | |
40 | the variable argument than ``__VA_ARGS__``. CPP permits | |
41 | this, as an extension. You may write an argument name immediately | |
42 | before the :samp:`...`; that name is used for the variable argument. | |
43 | The ``eprintf`` macro above could be written | |
44 | ||
45 | .. code-block:: c++ | |
46 | ||
47 | #define eprintf(args...) fprintf (stderr, args) | |
48 | ||
49 | using this extension. You cannot use ``__VA_ARGS__`` and this | |
50 | extension in the same macro. | |
51 | ||
52 | You can have named arguments as well as variable arguments in a variadic | |
53 | macro. We could define ``eprintf`` like this, instead: | |
54 | ||
55 | .. code-block:: c++ | |
56 | ||
57 | #define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__) | |
58 | ||
59 | This formulation looks more descriptive, but historically it was less | |
60 | flexible: you had to supply at least one argument after the format | |
61 | string. In standard C, you could not omit the comma separating the | |
62 | named argument from the variable arguments. (Note that this | |
63 | restriction has been lifted in C++20, and never existed in GNU C; see | |
64 | below.) | |
65 | ||
66 | Furthermore, if you left the variable argument empty, you would have | |
67 | gotten a syntax error, because there would have been an extra comma | |
68 | after the format string. | |
69 | ||
70 | .. code-block:: | |
71 | ||
72 | eprintf("success!\n", ); | |
73 | → fprintf(stderr, "success!\n", ); | |
74 | ||
75 | This has been fixed in C++20, and GNU CPP also has a pair of | |
76 | extensions which deal with this problem. | |
77 | ||
78 | First, in GNU CPP, and in C++ beginning in C++20, you are allowed to | |
79 | leave the variable argument out entirely: | |
80 | ||
81 | .. code-block:: | |
82 | ||
83 | eprintf ("success!\n") | |
84 | → fprintf(stderr, "success!\n", ); | |
85 | ||
86 | Second, C++20 introduces the ``__VA_OPT__`` function macro. | |
87 | This macro may only appear in the definition of a variadic macro. If | |
88 | the variable argument has any tokens, then a ``__VA_OPT__`` | |
89 | invocation expands to its argument; but if the variable argument does | |
90 | not have any tokens, the ``__VA_OPT__`` expands to nothing: | |
91 | ||
92 | .. code-block:: c++ | |
93 | ||
94 | #define eprintf(format, ...) \ | |
95 | fprintf (stderr, format __VA_OPT__(,) __VA_ARGS__) | |
96 | ||
97 | ``__VA_OPT__`` is also available in GNU C and GNU C++. | |
98 | ||
99 | Historically, GNU CPP has also had another extension to handle the | |
100 | trailing comma: the :samp:`##` token paste operator has a special | |
101 | meaning when placed between a comma and a variable argument. Despite | |
102 | the introduction of ``__VA_OPT__``, this extension remains | |
103 | supported in GNU CPP, for backward compatibility. If you write | |
104 | ||
105 | .. code-block:: c++ | |
106 | ||
107 | #define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__) | |
108 | ||
109 | and the variable argument is left out when the ``eprintf`` macro is | |
110 | used, then the comma before the :samp:`##` will be deleted. This does | |
111 | *not* happen if you pass an empty argument, nor does it happen if | |
112 | the token preceding :samp:`##` is anything other than a comma. | |
113 | ||
114 | .. code-block:: | |
115 | ||
116 | eprintf ("success!\n") | |
117 | → fprintf(stderr, "success!\n"); | |
118 | ||
119 | The above explanation is ambiguous about the case where the only macro | |
120 | parameter is a variable arguments parameter, as it is meaningless to | |
121 | try to distinguish whether no argument at all is an empty argument or | |
122 | a missing argument. | |
123 | CPP retains the comma when conforming to a specific C | |
124 | standard. Otherwise the comma is dropped as an extension to the standard. | |
125 | ||
126 | The C standard | |
127 | mandates that the only place the identifier ``__VA_ARGS__`` | |
128 | can appear is in the replacement list of a variadic macro. It may not | |
129 | be used as a macro name, macro argument name, or within a different type | |
130 | of macro. It may also be forbidden in open text; the standard is | |
131 | ambiguous. We recommend you avoid using it except for its defined | |
132 | purpose. | |
133 | ||
134 | Likewise, C++ forbids ``__VA_OPT__`` anywhere outside the | |
135 | replacement list of a variadic macro. | |
136 | ||
137 | Variadic macros became a standard part of the C language with C99. | |
138 | GNU CPP previously supported them | |
139 | with a named variable argument | |
140 | (:samp:`args...`, not :samp:`...` and ``__VA_ARGS__``), which | |
3ed1b4ce | 141 | is still supported for backward compatibility. |