Attribute specifier sequence(since 哋它亢23)
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 03 MINUTES 25 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
Syntax
[[
attr ]]
[[
attr1, attr2, attr3(
args)
]]
[[
attribute-prefix::
attr (
args)
]]
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 03 MINUTES 24 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
[[ attribute-list ]]
|
(since 哋它亢23) | ||||||||
where attribute-list is a comma-separated sequence of zero or more attribute-token s
standard-attribute | (1) | ||||||||
attribute-prefix :: identifier
|
(2) | ||||||||
standard-attribute ( argument-list (optional) )
|
(3) | ||||||||
attribute-prefix :: identifier ( argument-list (optional) )
|
(4) | ||||||||
where attribute-prefix is an identifier and argument-list is a sequence of tokens where parentheses, brackets and braces are balanced (balanced-token-sequence).
Explanation
Attributes provide the unified standard syntax for implementation-defined language extensions, such as the GNU and IBM language extensions __attribute__((...))
, Microsoft extension __declspec()
, etc.
An attribute can be used almost everywhere in the C program, and can be applied to almost everything: to types, to variables, to functions, to names, to code blocks, to entire translation units, although each particular attribute is only valid where it is permitted by the implementation: [[expect_true]]
could be an attribute that can only be used with an if, and not with a class declaration. [[omp::parallel()]]
could be an attribute that applies to a code block or to a for loop, but not to the type int
, etc. (note these two attributes are fictional examples, see below for the standard and some non-standard attributes)
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 03 MINUTES 23 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
Two consecutive left square bracket tokens ([[
) may only appear when introducing an attribute-specifier or inside an attribute argument.
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 03 MINUTES 22 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
Every standard-attribute is reserved for standardization. That is, every non-standard attribute is prefixed by a attribute-prefix provided by the implementation, e.g. [[gnu::may_alias]]
and [[clang::no_sanitize]]
.
Standard attributes
Only the following attributes are defined by the C standard. Every standard attribute whose name is of form attr
can be also spelled as __attr__
and its meaning is not changed.
Attribute testing
__has_c_attribute( attribute-token )
|
|||||||||
Checks for the presence of an attribute token named by attribute-token.
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 03 MINUTES 07 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
__has_c_attribute
can be expanded in the expression of MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 03 MINUTES 06 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE and MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 03 MINUTES 05 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE.
It is treated as a defined macro by MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 03 MINUTES 04 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE, MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 03 MINUTES 02 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE and MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 03 MINUTES 01 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE but cannot be used anywhere else.
Example
[[gnu::hot]] [[gnu::const]] [[nodiscard]] int f(void); // declare f with three attributes [[gnu::const, gnu::hot, nodiscard]] int f(void); // the same as above, but uses a single attr // specifier that contains three attributes int f(void) { return 0; } int main(void) { }
References
- 哋它亢23 standard (ISO/IEC 9899:2023):
- 6.7.12 Attributes (p: TBD)