26.5 __builtin_has_attribute Built-in Function

The __builtin_has_attribute built-in function evaluates to an integer constant expression. This expression is true if the symbol or type referenced by the built-in function's first argument has been declared with the attribute referenced by its second argument; false otherwise. Neither argument is evaluated. The first argument is subject to the same restrictions as the argument to typeof, and when it does not reference a symbol, the type of this expression is considered. This built-in function cannot be used as the argument to #if or #elif preprocessor directives.

The attribute argument is an attribute name optionally followed by a comma-separated list of attribute arguments enclosed in parentheses. Both forms of attribute names—with and without double leading and trailing underscores—are recognized. If no arguments are specified with an attribute that expects one or more arguments, the built-in function considers only if its first argument has been declared with the attribute and ignores any attribute argument. When an incomplete set of attribute arguments have been specified, the built-in function returns true if all specified arguments match.

For example, the first usage of the built-in function below evaluates to true because x is declared with the aligned attribute, but the second usage evaluates to false because x is declared aligned (8) and not aligned (4).
attribute ((aligned (8))) int x;

 _Static_assert (__builtin_has_attribute (x, aligned), "aligned");
 _Static_assert (!__builtin_has_attribute (x, aligned (4)), "aligned (4)");

Due to a limitation, the __builtin_has_attribute built-in function always returns false when checking for the mode attribute, even if that attribute was used with the declaration of the type or variable referenced by the first argument. The function does not support checking of labels or C enumerators as the first argument.

Prototype

bool __builtin_has_attribute (type-or-expression, attribute)

Argument

type-or-expression: the symbol to check

attribute: the attribute to check against

Return Value

A true or false value

Error Messages

None.