📄 extend.texi
字号:
@c Copyright (C) 1988, 1989, 1992, 1993, 1994, 1996, 1998, 1999, 2000, 2001,@c 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.@c This is part of the GCC manual.@c For copying conditions, see the file gcc.texi.@node C Extensions@chapter Extensions to the C Language Family@cindex extensions, C language@cindex C language extensions@opindex pedanticGNU C provides several language features not found in ISO standard C@.(The @option{-pedantic} option directs GCC to print a warning message ifany of these features is used.) To test for the availability of thesefeatures in conditional compilation, check for a predefined macro@code{__GNUC__}, which is always defined under GCC@.These extensions are available in C and Objective-C@. Most of them arealso available in C++. @xref{C++ Extensions,,Extensions to theC++ Language}, for extensions that apply @emph{only} to C++.Some features that are in ISO C99 but not C89 or C++ are also, asextensions, accepted by GCC in C89 mode and in C++.@menu* Statement Exprs:: Putting statements and declarations inside expressions.* Local Labels:: Labels local to a block.* Labels as Values:: Getting pointers to labels, and computed gotos.* Nested Functions:: As in Algol and Pascal, lexical scoping of functions.* Constructing Calls:: Dispatching a call to another function.* Typeof:: @code{typeof}: referring to the type of an expression.* Conditionals:: Omitting the middle operand of a @samp{?:} expression.* Long Long:: Double-word integers---@code{long long int}.* Complex:: Data types for complex numbers.* Floating Types:: Additional Floating Types.* Decimal Float:: Decimal Floating Types. * Hex Floats:: Hexadecimal floating-point constants.* Fixed-Point:: Fixed-Point Types.* Zero Length:: Zero-length arrays.* Variable Length:: Arrays whose length is computed at run time.* Empty Structures:: Structures with no members.* Variadic Macros:: Macros with a variable number of arguments.* Escaped Newlines:: Slightly looser rules for escaped newlines.* Subscripting:: Any array can be subscripted, even if not an lvalue.* Pointer Arith:: Arithmetic on @code{void}-pointers and function pointers.* Initializers:: Non-constant initializers.* Compound Literals:: Compound literals give structures, unions or arrays as values.* Designated Inits:: Labeling elements of initializers.* Cast to Union:: Casting to union type from any member of the union.* Case Ranges:: `case 1 ... 9' and such.* Mixed Declarations:: Mixing declarations and code.* Function Attributes:: Declaring that functions have no side effects, or that they can never return.* Attribute Syntax:: Formal syntax for attributes.* Function Prototypes:: Prototype declarations and old-style definitions.* C++ Comments:: C++ comments are recognized.* Dollar Signs:: Dollar sign is allowed in identifiers.* Character Escapes:: @samp{\e} stands for the character @key{ESC}.* Variable Attributes:: Specifying attributes of variables.* Type Attributes:: Specifying attributes of types.* Alignment:: Inquiring about the alignment of a type or variable.* Inline:: Defining inline functions (as fast as macros).* Extended Asm:: Assembler instructions with C expressions as operands. (With them you can define ``built-in'' functions.)* Constraints:: Constraints for asm operands* Asm Labels:: Specifying the assembler name to use for a C symbol.* Explicit Reg Vars:: Defining variables residing in specified registers.* Alternate Keywords:: @code{__const__}, @code{__asm__}, etc., for header files.* Incomplete Enums:: @code{enum foo;}, with details to follow.* Function Names:: Printable strings which are the name of the current function.* Return Address:: Getting the return or frame address of a function.* Vector Extensions:: Using vector instructions through built-in functions.* Offsetof:: Special syntax for implementing @code{offsetof}.* Atomic Builtins:: Built-in functions for atomic memory access.* Object Size Checking:: Built-in functions for limited buffer overflow checking.* Other Builtins:: Other built-in functions.* Target Builtins:: Built-in functions specific to particular targets.* Target Format Checks:: Format checks specific to particular targets.* Pragmas:: Pragmas accepted by GCC.* Unnamed Fields:: Unnamed struct/union fields within structs/unions.* Thread-Local:: Per-thread variables.* Binary constants:: Binary constants using the @samp{0b} prefix.@end menu@node Statement Exprs@section Statements and Declarations in Expressions@cindex statements inside expressions@cindex declarations inside expressions@cindex expressions containing statements@cindex macros, statements in expressions@c the above section title wrapped and causes an underfull hbox.. i@c changed it from "within" to "in". --mew 4feb93A compound statement enclosed in parentheses may appear as an expressionin GNU C@. This allows you to use loops, switches, and local variableswithin an expression.Recall that a compound statement is a sequence of statements surroundedby braces; in this construct, parentheses go around the braces. Forexample:@smallexample(@{ int y = foo (); int z; if (y > 0) z = y; else z = - y; z; @})@end smallexample@noindentis a valid (though slightly more complex than necessary) expressionfor the absolute value of @code{foo ()}.The last thing in the compound statement should be an expressionfollowed by a semicolon; the value of this subexpression serves as thevalue of the entire construct. (If you use some other kind of statementlast within the braces, the construct has type @code{void}, and thuseffectively no value.)This feature is especially useful in making macro definitions ``safe'' (sothat they evaluate each operand exactly once). For example, the``maximum'' function is commonly defined as a macro in standard C asfollows:@smallexample#define max(a,b) ((a) > (b) ? (a) : (b))@end smallexample@noindent@cindex side effects, macro argumentBut this definition computes either @var{a} or @var{b} twice, with badresults if the operand has side effects. In GNU C, if you know thetype of the operands (here taken as @code{int}), you can definethe macro safely as follows:@smallexample#define maxint(a,b) \ (@{int _a = (a), _b = (b); _a > _b ? _a : _b; @})@end smallexampleEmbedded statements are not allowed in constant expressions, such asthe value of an enumeration constant, the width of a bit-field, orthe initial value of a static variable.If you don't know the type of the operand, you can still do this, but youmust use @code{typeof} (@pxref{Typeof}).In G++, the result value of a statement expression undergoes array andfunction pointer decay, and is returned by value to the enclosingexpression. For instance, if @code{A} is a class, then@smallexample A a; (@{a;@}).Foo ()@end smallexample@noindentwill construct a temporary @code{A} object to hold the result of thestatement expression, and that will be used to invoke @code{Foo}.Therefore the @code{this} pointer observed by @code{Foo} will not be theaddress of @code{a}.Any temporaries created within a statement within a statement expressionwill be destroyed at the statement's end. This makes statementexpressions inside macros slightly different from function calls. Inthe latter case temporaries introduced during argument evaluation willbe destroyed at the end of the statement that includes the functioncall. In the statement expression case they will be destroyed duringthe statement expression. For instance,@smallexample#define macro(a) (@{__typeof__(a) b = (a); b + 3; @})template<typename T> T function(T a) @{ T b = a; return b + 3; @}void foo ()@{ macro (X ()); function (X ());@}@end smallexample@noindentwill have different places where temporaries are destroyed. For the@code{macro} case, the temporary @code{X} will be destroyed just afterthe initialization of @code{b}. In the @code{function} case thattemporary will be destroyed when the function returns.These considerations mean that it is probably a bad idea to usestatement-expressions of this form in header files that are designed towork with C++. (Note that some versions of the GNU C Library containedheader files using statement-expression that lead to precisely thisbug.)Jumping into a statement expression with @code{goto} or using a@code{switch} statement outside the statement expression with a@code{case} or @code{default} label inside the statement expression isnot permitted. Jumping into a statement expression with a computed@code{goto} (@pxref{Labels as Values}) yields undefined behavior.Jumping out of a statement expression is permitted, but if thestatement expression is part of a larger expression then it isunspecified which other subexpressions of that expression have beenevaluated except where the language definition requires certainsubexpressions to be evaluated before or after the statementexpression. In any case, as with a function call the evaluation of astatement expression is not interleaved with the evaluation of otherparts of the containing expression. For example,@smallexample foo (), ((@{ bar1 (); goto a; 0; @}) + bar2 ()), baz();@end smallexample@noindentwill call @code{foo} and @code{bar1} and will not call @code{baz} butmay or may not call @code{bar2}. If @code{bar2} is called, it will becalled after @code{foo} and before @code{bar1}@node Local Labels@section Locally Declared Labels@cindex local labels@cindex macros, local labelsGCC allows you to declare @dfn{local labels} in any nested blockscope. A local label is just like an ordinary label, but you canonly reference it (with a @code{goto} statement, or by taking itsaddress) within the block in which it was declared.A local label declaration looks like this:@smallexample__label__ @var{label};@end smallexample@noindentor@smallexample__label__ @var{label1}, @var{label2}, /* @r{@dots{}} */;@end smallexampleLocal label declarations must come at the beginning of the block,before any ordinary declarations or statements.The label declaration defines the label @emph{name}, but does not definethe label itself. You must do this in the usual way, with@code{@var{label}:}, within the statements of the statement expression.The local label feature is useful for complex macros. If a macrocontains nested loops, a @code{goto} can be useful for breaking out ofthem. However, an ordinary label whose scope is the whole functioncannot be used: if the macro can be expanded several times in onefunction, the label will be multiply defined in that function. Alocal label avoids this problem. For example:@smallexample#define SEARCH(value, array, target) \do @{ \ __label__ found; \ typeof (target) _SEARCH_target = (target); \ typeof (*(array)) *_SEARCH_array = (array); \ int i, j; \ int value; \ for (i = 0; i < max; i++) \ for (j = 0; j < max; j++) \ if (_SEARCH_array[i][j] == _SEARCH_target) \ @{ (value) = i; goto found; @} \ (value) = -1; \ found:; \@} while (0)@end smallexampleThis could also be written using a statement-expression:@smallexample#define SEARCH(array, target) \(@{ \ __label__ found; \ typeof (target) _SEARCH_target = (target); \ typeof (*(array)) *_SEARCH_array = (array); \ int i, j; \ int value; \ for (i = 0; i < max; i++) \ for (j = 0; j < max; j++) \ if (_SEARCH_array[i][j] == _SEARCH_target) \ @{ value = i; goto found; @} \ value = -1; \ found: \ value; \@})@end smallexampleLocal label declarations also make the labels they declare visible tonested functions, if there are any. @xref{Nested Functions}, for details.@node Labels as Values@section Labels as Values@cindex labels as values@cindex computed gotos@cindex goto with computed label@cindex address of a labelYou can get the address of a label defined in the current function(or a containing function) with the unary operator @samp{&&}. Thevalue has type @code{void *}. This value is a constant and can be usedwherever a constant of that type is valid. For example:@smallexamplevoid *ptr;/* @r{@dots{}} */ptr = &&foo;@end smallexampleTo use these values, you need to be able to jump to one. This is donewith the computed goto statement@footnote{The analogous feature inFortran is called an assigned goto, but that name seems inappropriate inC, where one can do more than simply store label addresses in labelvariables.}, @code{goto *@var{exp};}. For example,@smallexamplegoto *ptr;@end smallexample@noindentAny expression of type @code{void *} is allowed.One way of using these constants is in initializing a static array thatwill serve as a jump table:@smallexamplestatic void *array[] = @{ &&foo, &&bar, &&hack @};@end smallexampleThen you can select a label with indexing, like this:@smallexamplegoto *array[i];@end smallexample@noindentNote that this does not check whether the subscript is in bounds---arrayindexing in C never does that.Such an array of label values serves a purpose much like that of the@code{switch} statement. The @code{switch} statement is cleaner, souse that rather than an array unless the problem does not fit a@code{switch} statement very well.Another use of label values is in an interpreter for threaded code.The labels within the interpreter function can be stored in thethreaded code for super-fast dispatching.You may not use this mechanism to jump to code in a different function.If you do that, totally unpredictable things will happen. The best way toavoid this is to store the label address only in automatic variables andnever pass it as an argument.An alternate way to write the above example is@smallexamplestatic const int array[] = @{ &&foo - &&foo, &&bar - &&foo,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -