Content deleted Content added
Line 604:
== Attributes ==
Since C++11, C++ has supported attribute specifier sequences<ref>{{cite web|title=Attribute specifier sequence (since C++11)|url=https://cppreference.com/w/cpp/language/attributes.html|website=cppreference.com|access-date=6 June 2025}}</ref>. Attributes can be applied to any symbol that supports them, including classes, functions/methods, and variables, and any symbol marked with an attribute will be specifically treated by the compiler as necessary. These can be thought of as similar to [[Java annotation]]s for providing additional information to the compiler, however they differ in that attributes in C++ are not metadata that is meant to be accessed using reflection. Furthermore, one cannot create custom attributes in C++, unlike in Java where one may define custom annotations in addition to the standard ones. However, C++ does have implementation/vendor-specific attributes which are non-standard. These typically have a namespace associated with them. For instance, GCC and Clang have attributes under the <code>gnu::</code> namespace, and all such attributes are of the form {{
One may apply multiple attributes as a list, for instance {{
=== Standard attributes ===
Line 618:
! Name !! Description
|-
| {{
|| Indicates that the specified function will not return to its caller.
|-
| style="background:#F99" | {{
| style="background:#F99" | Indicates that the dependency chain in release-consume <code>std::memory_order</code> propagates in and out of the function. Removed since C++26.
|-
| {{
|| Indicates that the use of the marked symbol is allowed but discouraged/deprecated for the reason specified (if given).
|-
| {{
|| Indicates that the fall through from the previous case label is intentional.
|-
| {{
|| Suppresses compiler warnings on an unused entity.
|-
| {{
|| Issues a compiler warning if the return value of the marked symbol is discarded or ignored for the reason specified (if given).
|-
| {{
|| Indicates that the compiler should optimise for the case where a path of execution through a statement is more or less likely to occur than the other(s).
|-
| {{
|| Indicates that a non-static data member need not have an address distinct from all other non-static data members of its class.
|-
| {{
|| Indicates that the given expression always evaluates to
|-
| {{
|| Indicates that an object bears an indeterminate value if it is not initialised.
|-
| {{
|| Indicates that a function is stateless, effectless, idempotent and independent.
|-
| {{
|| Indicates that a function is effectless and idempotent.
|}
=== Scoped attributes ===
As mentioned previously, GCC and Clang have scoped (namespaced) attributes, such as {{
<syntaxhighlight lang="cpp">
[[gnu::always_inline]] [[gnu::hot]] [[gnu::const]] [[nodiscard]]
|