Function attributes are orthogonal extensions, implemented to enhance the portability of programs developed with GNU C. Specifiable attributes for functions provide explicit ways to help the compiler optimize function calls and to instruct it to check more aspects of the code. Others provide additional functionality.
IBM C and C++ implement a subset of the GNU C function attributes. If a particular function attribute is not implemented, its specification is accepted and the semantics are ignored. These language features are collectively available when compiling in any of the extended language levels.
The IBM language extensions for function attributes preserve the GNU C syntax. A function attribute specification using the form __attribute_name__ (that is, the function attribute keyword with double underscore characters leading and trailing) reduces the likelihood of a name conflict with a macro of the same name.
The keyword __attribute__ introduces an attribute specifier. Some of the attributes can also be applied to variables. The syntax is of the general form:
.-,---------------------------------. V | >>-__attribute__--((----+-------------------------------+-+--))->< +-individual_attribute_name-----+ '-__individual_attribute_name__-'
Function attributes are attached to a declarator.
For attributes specified on a function prototype declaration, attaching them
to the declarator means placing them after the closing parenthesis of the
parameter list.
/* Specify the attribute on a function prototype declaration */ void f(int i, int j) __attribute__((individual_attribute_name)); void f(int i, int j) { }
Due to ambiguities in parsing old-style parameter declarations, a function
definition must have the attribute specification precede the
declarator. For example, the following definitions of foo
show the correct placement:
int __attribute__((individual_attribute_name)) foo(int i) { } int __attribute__((individual_attribute_name)) foo(i,j) int i; int j; { }
For C++, function attributes are placed after the declarator on either
declarations or definitions. For typical functions, this is also after
the closing parenthesis; however, function attributes must follow any
exception specification that may be present for the function.
Related References