Next: , Previous: , Up: Particular Modules   [Contents][Index]


17.4 Attributes

This module provides a header file attribute.h that defines macros related to C and C++ attributes and the GCC __attribute__ keyword.

Here is an example of its use:

#include <attribute.h>

NODISCARD
extern char *crypt (char const *, char const *)
  ATTRIBUTE_NOTHROW ATTRIBUTE_LEAF ATTRIBUTE_NONNULL ((1, 2));

NODISCARD expands to [[nodiscard]] if the compiler supports this C23 syntax, otherwise to __attribute__ ((__warn_unused_result__)) if the compiler is a recent-enough GCC or GCC-like compiler, otherwise to nothing. ATTRIBUTE_NOTHROW expands to __attribute__ ((__nothrow__)) if the compiler is a recent-enough GCC or GCC-like compiler, and to nothing otherwise. Similarly for ATTRIBUTE_LEAF. ATTRIBUTE_NONNULL ((1, 2)) expands to __attribute__ ((__nonnull__ (1, 2))) if the compiler is recent-enough GCC, and to nothing otherwise.

Most of these attribute names begin with ATTRIBUTE_. A few do not, because they are part of C23 and their names are not likely to clash with other macro names. These macros are DEPRECATED, FALLTHROUGH, MAYBE_UNUSED, and NODISCARD, which can be defined to [[deprecated]] etc. on C23 platforms. Also, these exceptional macros should be placed at the start of function declarations, whereas the ATTRIBUTE_* macros can be placed at the end.