Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Variadic-Macros.html 10KB

vor 3 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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-2020 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.5, 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#Top" rel="start" title="Top">
  27. <link href="Index-of-Directives.html#Index-of-Directives" rel="index" title="Index of Directives">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Macros.html#Macros" rel="up" title="Macros">
  30. <link href="Predefined-Macros.html#Predefined-Macros" rel="next" title="Predefined Macros">
  31. <link href="Concatenation.html#Concatenation" rel="prev" title="Concatenation">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style: oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Variadic-Macros"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Predefined-Macros.html#Predefined-Macros" accesskey="n" rel="next">Predefined Macros</a>, Previous: <a href="Concatenation.html#Concatenation" accesskey="p" rel="prev">Concatenation</a>, Up: <a href="Macros.html#Macros" 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#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
  65. </div>
  66. <hr>
  67. <a name="Variadic-Macros-1"></a>
  68. <h3 class="section">3.6 Variadic Macros</h3>
  69. <a name="index-variable-number-of-arguments"></a>
  70. <a name="index-macros-with-variable-arguments"></a>
  71. <a name="index-variadic-macros"></a>
  72. <p>A macro can be declared to accept a variable number of arguments much as
  73. a function can. The syntax for defining the macro is similar to that of
  74. a function. Here is an example:
  75. </p>
  76. <div class="smallexample">
  77. <pre class="smallexample">#define eprintf(...) fprintf (stderr, __VA_ARGS__)
  78. </pre></div>
  79. <p>This kind of macro is called <em>variadic</em>. When the macro is invoked,
  80. all the tokens in its argument list after the last named argument (this
  81. macro has none), including any commas, become the <em>variable
  82. argument</em>. This sequence of tokens replaces the identifier
  83. <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code> in the macro body wherever it appears. Thus, we
  84. have this expansion:
  85. </p>
  86. <div class="smallexample">
  87. <pre class="smallexample">eprintf (&quot;%s:%d: &quot;, input_file, lineno)
  88. &rarr; fprintf (stderr, &quot;%s:%d: &quot;, input_file, lineno)
  89. </pre></div>
  90. <p>The variable argument is completely macro-expanded before it is inserted
  91. into the macro expansion, just like an ordinary argument. You may use
  92. the &lsquo;<samp>#</samp>&rsquo; and &lsquo;<samp>##</samp>&rsquo; operators to stringize the variable argument
  93. or to paste its leading or trailing token with another token. (But see
  94. below for an important special case for &lsquo;<samp>##</samp>&rsquo;.)
  95. </p>
  96. <p>If your macro is complicated, you may want a more descriptive name for
  97. the variable argument than <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>. CPP permits
  98. this, as an extension. You may write an argument name immediately
  99. before the &lsquo;<samp>...</samp>&rsquo;; that name is used for the variable argument.
  100. The <code>eprintf</code> macro above could be written
  101. </p>
  102. <div class="smallexample">
  103. <pre class="smallexample">#define eprintf(args...) fprintf (stderr, args)
  104. </pre></div>
  105. <p>using this extension. You cannot use <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code> and this
  106. extension in the same macro.
  107. </p>
  108. <p>You can have named arguments as well as variable arguments in a variadic
  109. macro. We could define <code>eprintf</code> like this, instead:
  110. </p>
  111. <div class="smallexample">
  112. <pre class="smallexample">#define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__)
  113. </pre></div>
  114. <p>This formulation looks more descriptive, but historically it was less
  115. flexible: you had to supply at least one argument after the format
  116. string. In standard C, you could not omit the comma separating the
  117. named argument from the variable arguments. (Note that this
  118. restriction has been lifted in C++2a, and never existed in GNU C; see
  119. below.)
  120. </p>
  121. <p>Furthermore, if you left the variable argument empty, you would have
  122. gotten a syntax error, because there would have been an extra comma
  123. after the format string.
  124. </p>
  125. <div class="smallexample">
  126. <pre class="smallexample">eprintf(&quot;success!\n&quot;, );
  127. &rarr; fprintf(stderr, &quot;success!\n&quot;, );
  128. </pre></div>
  129. <p>This has been fixed in C++2a, and GNU CPP also has a pair of
  130. extensions which deal with this problem.
  131. </p>
  132. <p>First, in GNU CPP, and in C++ beginning in C++2a, you are allowed to
  133. leave the variable argument out entirely:
  134. </p>
  135. <div class="smallexample">
  136. <pre class="smallexample">eprintf (&quot;success!\n&quot;)
  137. &rarr; fprintf(stderr, &quot;success!\n&quot;, );
  138. </pre></div>
  139. <p>Second, C++2a introduces the <code><span class="nolinebreak">__VA_OPT__</span><!-- /@w --></code> function macro.
  140. This macro may only appear in the definition of a variadic macro. If
  141. the variable argument has any tokens, then a <code><span class="nolinebreak">__VA_OPT__</span><!-- /@w --></code>
  142. invocation expands to its argument; but if the variable argument does
  143. not have any tokens, the <code><span class="nolinebreak">__VA_OPT__</span><!-- /@w --></code> expands to nothing:
  144. </p>
  145. <div class="smallexample">
  146. <pre class="smallexample">#define eprintf(format, ...) \
  147. fprintf (stderr, format __VA_OPT__(,) __VA_ARGS__)
  148. </pre></div>
  149. <p><code><span class="nolinebreak">__VA_OPT__</span><!-- /@w --></code> is also available in GNU C and GNU C++.
  150. </p>
  151. <p>Historically, GNU CPP has also had another extension to handle the
  152. trailing comma: the &lsquo;<samp>##</samp>&rsquo; token paste operator has a special
  153. meaning when placed between a comma and a variable argument. Despite
  154. the introduction of <code><span class="nolinebreak">__VA_OPT__</span><!-- /@w --></code>, this extension remains
  155. supported in GNU CPP, for backward compatibility. If you write
  156. </p>
  157. <div class="smallexample">
  158. <pre class="smallexample">#define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__)
  159. </pre></div>
  160. <p>and the variable argument is left out when the <code>eprintf</code> macro is
  161. used, then the comma before the &lsquo;<samp>##</samp>&rsquo; will be deleted. This does
  162. <em>not</em> happen if you pass an empty argument, nor does it happen if
  163. the token preceding &lsquo;<samp>##</samp>&rsquo; is anything other than a comma.
  164. </p>
  165. <div class="smallexample">
  166. <pre class="smallexample">eprintf (&quot;success!\n&quot;)
  167. &rarr; fprintf(stderr, &quot;success!\n&quot;);
  168. </pre></div>
  169. <p>The above explanation is ambiguous about the case where the only macro
  170. parameter is a variable arguments parameter, as it is meaningless to
  171. try to distinguish whether no argument at all is an empty argument or
  172. a missing argument.
  173. CPP retains the comma when conforming to a specific C
  174. standard. Otherwise the comma is dropped as an extension to the standard.
  175. </p>
  176. <p>The C standard
  177. mandates that the only place the identifier <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>
  178. can appear is in the replacement list of a variadic macro. It may not
  179. be used as a macro name, macro argument name, or within a different type
  180. of macro. It may also be forbidden in open text; the standard is
  181. ambiguous. We recommend you avoid using it except for its defined
  182. purpose.
  183. </p>
  184. <p>Likewise, C++ forbids <code><span class="nolinebreak">__VA_OPT__</span><!-- /@w --></code> anywhere outside the
  185. replacement list of a variadic macro.
  186. </p>
  187. <p>Variadic macros became a standard part of the C language with C99.
  188. GNU CPP previously supported them
  189. with a named variable argument
  190. (&lsquo;<samp>args...</samp>&rsquo;, not &lsquo;<samp>...</samp>&rsquo; and <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>), which
  191. is still supported for backward compatibility.
  192. </p>
  193. <hr>
  194. <div class="header">
  195. <p>
  196. Next: <a href="Predefined-Macros.html#Predefined-Macros" accesskey="n" rel="next">Predefined Macros</a>, Previous: <a href="Concatenation.html#Concatenation" accesskey="p" rel="prev">Concatenation</a>, Up: <a href="Macros.html#Macros" 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#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
  197. </div>
  198. </body>
  199. </html>