Variadic-Macros.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1987-2023 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation. A copy of
  7. the license is included in the
  8. section entitled "GNU Free Documentation License".
  9. This manual contains no Invariant Sections. The Front-Cover Texts are
  10. (a) (see below), and the Back-Cover Texts are (b) (see below).
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Variadic Macros (The C Preprocessor)</title>
  21. <meta name="description" content="Variadic Macros (The C Preprocessor)">
  22. <meta name="keywords" content="Variadic Macros (The C Preprocessor)">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html" rel="start" title="Top">
  27. <link href="Index-of-Directives.html" rel="index" title="Index of Directives">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Macros.html" rel="up" title="Macros">
  30. <link href="Predefined-Macros.html" rel="next" title="Predefined Macros">
  31. <link href="Concatenation.html" rel="prev" title="Concatenation">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.lisp {margin-left: 3.2em}
  39. kbd {font-style: oblique}
  40. pre.display {font-family: inherit}
  41. pre.format {font-family: inherit}
  42. pre.menu-comment {font-family: serif}
  43. pre.menu-preformatted {font-family: serif}
  44. span.nolinebreak {white-space: nowrap}
  45. span.roman {font-family: initial; font-weight: normal}
  46. span.sansserif {font-family: sans-serif; font-weight: normal}
  47. ul.no-bullet {list-style: none}
  48. -->
  49. </style>
  50. </head>
  51. <body lang="en">
  52. <span id="Variadic-Macros"></span><div class="header">
  53. <p>
  54. Next: <a href="Predefined-Macros.html" accesskey="n" rel="next">Predefined Macros</a>, Previous: <a href="Concatenation.html" accesskey="p" rel="prev">Concatenation</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html" title="Index" rel="index">Index</a>]</p>
  55. </div>
  56. <hr>
  57. <span id="Variadic-Macros-1"></span><h3 class="section">3.6 Variadic Macros</h3>
  58. <span id="index-variable-number-of-arguments"></span>
  59. <span id="index-macros-with-variable-arguments"></span>
  60. <span id="index-variadic-macros"></span>
  61. <p>A macro can be declared to accept a variable number of arguments much as
  62. a function can. The syntax for defining the macro is similar to that of
  63. a function. Here is an example:
  64. </p>
  65. <div class="example">
  66. <pre class="example">#define eprintf(...) fprintf (stderr, __VA_ARGS__)
  67. </pre></div>
  68. <p>This kind of macro is called <em>variadic</em>. When the macro is invoked,
  69. all the tokens in its argument list after the last named argument (this
  70. macro has none), including any commas, become the <em>variable
  71. argument</em>. This sequence of tokens replaces the identifier
  72. <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code> in the macro body wherever it appears. Thus, we
  73. have this expansion:
  74. </p>
  75. <div class="example">
  76. <pre class="example">eprintf (&quot;%s:%d: &quot;, input_file, lineno)
  77. &rarr; fprintf (stderr, &quot;%s:%d: &quot;, input_file, lineno)
  78. </pre></div>
  79. <p>The variable argument is completely macro-expanded before it is inserted
  80. into the macro expansion, just like an ordinary argument. You may use
  81. the &lsquo;<samp>#</samp>&rsquo; and &lsquo;<samp>##</samp>&rsquo; operators to stringize the variable argument
  82. or to paste its leading or trailing token with another token. (But see
  83. below for an important special case for &lsquo;<samp>##</samp>&rsquo;.)
  84. </p>
  85. <p>If your macro is complicated, you may want a more descriptive name for
  86. the variable argument than <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>. CPP permits
  87. this, as an extension. You may write an argument name immediately
  88. before the &lsquo;<samp>...</samp>&rsquo;; that name is used for the variable argument.
  89. The <code>eprintf</code> macro above could be written
  90. </p>
  91. <div class="example">
  92. <pre class="example">#define eprintf(args...) fprintf (stderr, args)
  93. </pre></div>
  94. <p>using this extension. You cannot use <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code> and this
  95. extension in the same macro.
  96. </p>
  97. <p>You can have named arguments as well as variable arguments in a variadic
  98. macro. We could define <code>eprintf</code> like this, instead:
  99. </p>
  100. <div class="example">
  101. <pre class="example">#define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__)
  102. </pre></div>
  103. <p>This formulation looks more descriptive, but historically it was less
  104. flexible: you had to supply at least one argument after the format
  105. string. In standard C, you could not omit the comma separating the
  106. named argument from the variable arguments. (Note that this
  107. restriction has been lifted in C++20, and never existed in GNU C; see
  108. below.)
  109. </p>
  110. <p>Furthermore, if you left the variable argument empty, you would have
  111. gotten a syntax error, because there would have been an extra comma
  112. after the format string.
  113. </p>
  114. <div class="example">
  115. <pre class="example">eprintf(&quot;success!\n&quot;, );
  116. &rarr; fprintf(stderr, &quot;success!\n&quot;, );
  117. </pre></div>
  118. <p>This has been fixed in C++20, and GNU CPP also has a pair of
  119. extensions which deal with this problem.
  120. </p>
  121. <p>First, in GNU CPP, and in C++ beginning in C++20, you are allowed to
  122. leave the variable argument out entirely:
  123. </p>
  124. <div class="example">
  125. <pre class="example">eprintf (&quot;success!\n&quot;)
  126. &rarr; fprintf(stderr, &quot;success!\n&quot;, );
  127. </pre></div>
  128. <p>Second, C++20 introduces the <code><span class="nolinebreak">__VA_OPT__</span><!-- /@w --></code> function macro.
  129. This macro may only appear in the definition of a variadic macro. If
  130. the variable argument has any tokens, then a <code><span class="nolinebreak">__VA_OPT__</span><!-- /@w --></code>
  131. invocation expands to its argument; but if the variable argument does
  132. not have any tokens, the <code><span class="nolinebreak">__VA_OPT__</span><!-- /@w --></code> expands to nothing:
  133. </p>
  134. <div class="example">
  135. <pre class="example">#define eprintf(format, ...) \
  136. fprintf (stderr, format __VA_OPT__(,) __VA_ARGS__)
  137. </pre></div>
  138. <p><code><span class="nolinebreak">__VA_OPT__</span><!-- /@w --></code> is also available in GNU C and GNU C++.
  139. </p>
  140. <p>Historically, GNU CPP has also had another extension to handle the
  141. trailing comma: the &lsquo;<samp>##</samp>&rsquo; token paste operator has a special
  142. meaning when placed between a comma and a variable argument. Despite
  143. the introduction of <code><span class="nolinebreak">__VA_OPT__</span><!-- /@w --></code>, this extension remains
  144. supported in GNU CPP, for backward compatibility. If you write
  145. </p>
  146. <div class="example">
  147. <pre class="example">#define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__)
  148. </pre></div>
  149. <p>and the variable argument is left out when the <code>eprintf</code> macro is
  150. used, then the comma before the &lsquo;<samp>##</samp>&rsquo; will be deleted. This does
  151. <em>not</em> happen if you pass an empty argument, nor does it happen if
  152. the token preceding &lsquo;<samp>##</samp>&rsquo; is anything other than a comma.
  153. </p>
  154. <div class="example">
  155. <pre class="example">eprintf (&quot;success!\n&quot;)
  156. &rarr; fprintf(stderr, &quot;success!\n&quot;);
  157. </pre></div>
  158. <p>The above explanation is ambiguous about the case where the only macro
  159. parameter is a variable arguments parameter, as it is meaningless to
  160. try to distinguish whether no argument at all is an empty argument or
  161. a missing argument.
  162. CPP retains the comma when conforming to a specific C
  163. standard. Otherwise the comma is dropped as an extension to the standard.
  164. </p>
  165. <p>The C standard
  166. mandates that the only place the identifier <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>
  167. can appear is in the replacement list of a variadic macro. It may not
  168. be used as a macro name, macro argument name, or within a different type
  169. of macro. It may also be forbidden in open text; the standard is
  170. ambiguous. We recommend you avoid using it except for its defined
  171. purpose.
  172. </p>
  173. <p>Likewise, C++ forbids <code><span class="nolinebreak">__VA_OPT__</span><!-- /@w --></code> anywhere outside the
  174. replacement list of a variadic macro.
  175. </p>
  176. <p>Variadic macros became a standard part of the C language with C99.
  177. GNU CPP previously supported them
  178. with a named variable argument
  179. (&lsquo;<samp>args...</samp>&rsquo;, not &lsquo;<samp>...</samp>&rsquo; and <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>), which
  180. is still supported for backward compatibility.
  181. </p>
  182. <hr>
  183. <div class="header">
  184. <p>
  185. Next: <a href="Predefined-Macros.html" accesskey="n" rel="next">Predefined Macros</a>, Previous: <a href="Concatenation.html" accesskey="p" rel="prev">Concatenation</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html" title="Index" rel="index">Index</a>]</p>
  186. </div>
  187. </body>
  188. </html>