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