⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp.texi

📁 理解和实践操作系统的一本好书
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
@end smallexample@noindentpasses two arguments to @code{macro}: @code{array[x = y} and @code{x +1]}.  If you want to supply @code{array[x = y, x + 1]} as an argument,you can write it as @code{array[(x = y, x + 1)]}, which is equivalent Ccode.All arguments to a macro are completely macro-expanded before they aresubstituted into the macro body.  After substitution, the complete textis scanned again for macros to expand, including the arguments.  This rulemay seem strange, but it is carefully designed so you need not worryabout whether any function call is actually a macro invocation.  You canrun into trouble if you try to be too clever, though.  @xref{ArgumentPrescan}, for detailed discussion.For example, @code{min (min (a, b), c)} is first expanded to@smallexample  min (((a) < (b) ? (a) : (b)), (c))@end smallexample@noindentand then to@smallexample@group((((a) < (b) ? (a) : (b))) < (c) ? (((a) < (b) ? (a) : (b))) : (c))@end group@end smallexample@noindent(Line breaks shown here for clarity would not actually be generated.)@cindex empty macro argumentsYou can leave macro arguments empty; this is not an error to thepreprocessor (but many macros will then expand to invalid code).You cannot leave out arguments entirely; if a macro takes two arguments,there must be exactly one comma at the top level of its argument list.Here are some silly examples using @code{min}:@smallexamplemin(, b)        @expansion{} ((   ) < (b) ? (   ) : (b))min(a, )        @expansion{} ((a  ) < ( ) ? (a  ) : ( ))min(,)          @expansion{} ((   ) < ( ) ? (   ) : ( ))min((,),)       @expansion{} (((,)) < ( ) ? ((,)) : ( ))min()      @error{} macro "min" requires 2 arguments, but only 1 givenmin(,,)    @error{} macro "min" passed 3 arguments, but takes just 2@end smallexampleWhitespace is not a preprocessing token, so if a macro @code{foo} takesone argument, @code{@w{foo ()}} and @code{@w{foo ( )}} both supply it anempty argument.  Previous GNU preprocessor implementations anddocumentation were incorrect on this point, insisting that afunction-like macro that takes a single argument be passed a space if anempty argument was required.Macro parameters appearing inside string literals are not replaced bytheir corresponding actual arguments.@smallexample#define foo(x) x, "x"foo(bar)        @expansion{} bar, "x"@end smallexample@node Stringification@section Stringification@cindex stringification@cindex @samp{#} operatorSometimes you may want to convert a macro argument into a stringconstant.  Parameters are not replaced inside string constants, but youcan use the @samp{#} preprocessing operator instead.  When a macroparameter is used with a leading @samp{#}, the preprocessor replaces itwith the literal text of the actual argument, converted to a stringconstant.  Unlike normal parameter replacement, the argument is notmacro-expanded first.  This is called @dfn{stringification}.There is no way to combine an argument with surrounding text andstringify it all together.  Instead, you can write a series of adjacentstring constants and stringified arguments.  The preprocessor willreplace the stringified arguments with string constants.  The Ccompiler will then combine all the adjacent string constants into onelong string.Here is an example of a macro definition that uses stringification:@smallexample@group#define WARN_IF(EXP) \do @{ if (EXP) \        fprintf (stderr, "Warning: " #EXP "\n"); @} \while (0)WARN_IF (x == 0);     @expansion{} do @{ if (x == 0)           fprintf (stderr, "Warning: " "x == 0" "\n"); @} while (0);@end group@end smallexample@noindentThe argument for @code{EXP} is substituted once, as-is, into the@code{if} statement, and once, stringified, into the argument to@code{fprintf}.  If @code{x} were a macro, it would be expanded in the@code{if} statement, but not in the string.The @code{do} and @code{while (0)} are a kludge to make it possible towrite @code{WARN_IF (@var{arg});}, which the resemblance of@code{WARN_IF} to a function would make C programmers want to do; see@ref{Swallowing the Semicolon}.Stringification in C involves more than putting double-quote charactersaround the fragment.  The preprocessor backslash-escapes the quotessurrounding embedded string constants, and all backslashes within string andcharacter constants, in order to get a valid C string constant with theproper contents.  Thus, stringifying @code{@w{p = "foo\n";}} results in@t{@w{"p = \"foo\\n\";"}}.  However, backslashes that are not inside stringor character constants are not duplicated: @samp{\n} by itselfstringifies to @t{"\n"}.All leading and trailing whitespace in text being stringified isignored.  Any sequence of whitespace in the middle of the text isconverted to a single space in the stringified result.  Comments arereplaced by whitespace long before stringification happens, so theynever appear in stringified text.There is no way to convert a macro argument into a character constant.If you want to stringify the result of expansion of a macro argument,you have to use two levels of macros.@smallexample#define xstr(s) str(s)#define str(s) #s#define foo 4str (foo)     @expansion{} "foo"xstr (foo)     @expansion{} xstr (4)     @expansion{} str (4)     @expansion{} "4"@end smallexample@code{s} is stringified when it is used in @code{str}, so it is notmacro-expanded first.  But @code{s} is an ordinary argument to@code{xstr}, so it is completely macro-expanded before @code{xstr}itself is expanded (@pxref{Argument Prescan}).  Therefore, by the time@code{str} gets to its argument, it has already been macro-expanded.@node Concatenation@section Concatenation@cindex concatenation@cindex token pasting@cindex token concatenation@cindex @samp{##} operatorIt is often useful to merge two tokens into one while expanding macros.This is called @dfn{token pasting} or @dfn{token concatenation}.  The@samp{##} preprocessing operator performs token pasting.  When a macrois expanded, the two tokens on either side of each @samp{##} operatorare combined into a single token, which then replaces the @samp{##} andthe two original tokens in the macro expansion.  Usually both will beidentifiers, or one will be an identifier and the other a preprocessingnumber.  When pasted, they make a longer identifier.  This isn't theonly valid case.  It is also possible to concatenate two numbers (or anumber and a name, such as @code{1.5} and @code{e3}) into a number.Also, multi-character operators such as @code{+=} can be formed bytoken pasting.However, two tokens that don't together form a valid token cannot bepasted together.  For example, you cannot concatenate @code{x} with@code{+} in either order.  If you try, the preprocessor issues a warningand emits the two tokens.  Whether it puts white space between thetokens is undefined.  It is common to find unnecessary uses of @samp{##}in complex macros.  If you get this warning, it is likely that you cansimply remove the @samp{##}.Both the tokens combined by @samp{##} could come from the macro body,but you could just as well write them as one token in the first place.Token pasting is most useful when one or both of the tokens comes from amacro argument.  If either of the tokens next to an @samp{##} is aparameter name, it is replaced by its actual argument before @samp{##}executes.  As with stringification, the actual argument is notmacro-expanded first.  If the argument is empty, that @samp{##} has noeffect.Keep in mind that the C preprocessor converts comments to whitespacebefore macros are even considered.  Therefore, you cannot create acomment by concatenating @samp{/} and @samp{*}.  You can put as muchwhitespace between @samp{##} and its operands as you like, includingcomments, and you can put comments in arguments that will beconcatenated.  However, it is an error if @samp{##} appears at eitherend of a macro body.Consider a C program that interprets named commands.  There probablyneeds to be a table of commands, perhaps an array of structures declaredas follows:@smallexample@groupstruct command@{  char *name;  void (*function) (void);@};@end group@groupstruct command commands[] =@{  @{ "quit", quit_command @},  @{ "help", help_command @},  @dots{}@};@end group@end smallexampleIt would be cleaner not to have to give each command name twice, once inthe string constant and once in the function name.  A macro which takes thename of a command as an argument can make this unnecessary.  The stringconstant can be created with stringification, and the function name byconcatenating the argument with @samp{_command}.  Here is how it is done:@smallexample#define COMMAND(NAME)  @{ #NAME, NAME ## _command @}struct command commands[] =@{  COMMAND (quit),  COMMAND (help),  @dots{}@};@end smallexample@node Variadic Macros@section Variadic Macros@cindex variable number of arguments@cindex macros with variable arguments@cindex variadic macrosA macro can be declared to accept a variable number of arguments much asa function can.  The syntax for defining the macro is similar to that ofa function.  Here is an example:@smallexample#define eprintf(@dots{}) fprintf (stderr, __VA_ARGS__)@end smallexampleThis kind of macro is called @dfn{variadic}.  When the macro is invoked,all the tokens in its argument list after the last named argument (thismacro has none), including any commas, become the @dfn{variableargument}.  This sequence of tokens replaces the identifier@code{@w{__VA_ARGS__}} in the macro body wherever it appears.  Thus, wehave this expansion:@smallexampleeprintf ("%s:%d: ", input_file, lineno)     @expansion{}  fprintf (stderr, "%s:%d: ", input_file, lineno)@end smallexampleThe variable argument is completely macro-expanded before it is insertedinto the macro expansion, just like an ordinary argument.  You may usethe @samp{#} and @samp{##} operators to stringify the variable argumentor to paste its leading or trailing token with another token.  (But seebelow for an important special case for @samp{##}.)If your macro is complicated, you may want a more descriptive name forthe variable argument than @code{@w{__VA_ARGS__}}.  CPP permitsthis, as an extension.  You may write an argument name immediatelybefore the @samp{@dots{}}; that name is used for the variable argument.The @code{eprintf} macro above could be written@smallexample#define eprintf(args@dots{}) fprintf (stderr, args)@end smallexample@noindentusing this extension.  You cannot use @code{@w{__VA_ARGS__}} and thisextension in the same macro.You can have named arguments as well as variable arguments in a variadicmacro.  We could define @code{eprintf} like this, instead:@smallexample#define eprintf(format, @dots{}) fprintf (stderr, format, __VA_ARGS__)@end smallexample@noindentThis formulation looks more descriptive, but unfortunately it is lessflexible: you must now supply at least one argument after the formatstring.  In standard C, you cannot omit the comma separating the namedargument from the variable arguments.  Furthermore, if you leave thevariable argument empty, you will get a syntax error, becausethere will be an extra comma after the format string.@smallexampleeprintf("success!\n", );     @expansion{} fprintf(stderr, "success!\n", );@end smallexampleGNU CPP has a pair of extensions which deal with this problem.  First,you are allowed to leave the variable argument out entirely:@smallexampleeprintf ("success!\n")     @expansion{} fprintf(stderr, "success!\n", );@end smallexample@noindentSecond, the @samp{##} token paste operator has a special meaning whenplaced between a comma and a variable argument.  If you write@smallexample#define eprintf(format, @dots{}) fprintf (stderr, format, ##__VA_ARGS__)@end smallexample@noindentand the variable argument is left out when the @code{eprintf} macro isused, then the comma before the @samp{##} will be deleted.  This does@emph{not} happen if you pass an empty argument, nor does it happen ifthe token preceding @samp{##} is anything other than a comma.@smallexampleeprintf ("success!\n")     @expansion{} fprintf(stderr, "success!\n");@end smallexample@noindentThe above explanation is ambiguous about the case where the only macroparameter is a variable arguments parameter, as it is meaningless totry to distinguish whether no argument at all is an empty argument ora missing argument.  In this case the C99 standard is clear that thecomma must remain, however the existing GCC extension used to swallowthe comma.  So CPP retains the comma when conforming to a specific Cstandard, and drops it otherwise.C99 mandates that the only place the identifier @code{@w{__VA_ARGS__}}can appear is in the replacement list of a variadic macro.  It may notbe used as a macro name, macro argument name, or within a different typeof macro.  It may also be forbidden in open text; the standard isambiguous.  We recommend you avoid using it except for its definedpurp

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -