__has_attribute
¶The special operator __has_attribute (operand)
may be used
in ‘#if’ and ‘#elif’ expressions to test whether the attribute
referenced by its operand is recognized by GCC. Using the operator
in other contexts is not valid. In C code, if compiling for strict
conformance to standards before C2x, operand must be
a valid identifier. Otherwise, operand may be optionally
introduced by the attribute-scope::
prefix.
The attribute-scope prefix identifies the “namespace” within
which the attribute is recognized. The scope of GCC attributes is
‘gnu’ or ‘__gnu__’. The __has_attribute
operator by
itself, without any operand or parentheses, acts as a predefined
macro so that support for it can be tested in portable code. Thus,
the recommended use of the operator is as follows:
#if defined __has_attribute # if __has_attribute (nonnull) # define ATTR_NONNULL __attribute__ ((nonnull)) # endif #endif
The first ‘#if’ test succeeds only when the operator is supported
by the version of GCC (or another compiler) being used. Only when that
test succeeds is it valid to use __has_attribute
as a preprocessor
operator. As a result, combining the two tests into a single expression as
shown below would only be valid with a compiler that supports the operator
but not with others that don’t.
#if defined __has_attribute && __has_attribute (nonnull) /* not portable */ … #endif