C++ syntax: Difference between revisions

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 {{codecpp|[[gnu::*]]}}.
 
One may apply multiple attributes as a list, for instance {{codecpp|[[A, B, C]]}} (where <code>A</code>, <code>B</code>, and <code>C</code> are attributes). Furthermore, attributes may also accept arguments, like {{codecpp|[[A("This is a parameter")]]}}.
 
=== Standard attributes ===
Line 618:
! Name !! Description
|-
| {{codecpp|[[noreturn]]}}
|| Indicates that the specified function will not return to its caller.
|-
| style="background:#F99" | {{codecpp|[[carries_dependency]]}}
| 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.
|-
| {{codecpp|[[deprecated]]}}<br>{{codecpp|[[deprecated("reason")]]}}
|| Indicates that the use of the marked symbol is allowed but discouraged/deprecated for the reason specified (if given).
|-
| {{codecpp|[[fallthrough]]}}
|| Indicates that the fall through from the previous case label is intentional.
|-
| {{codecpp|[[maybe_unused]]}}
|| Suppresses compiler warnings on an unused entity.
|-
| {{codecpp|[[nodiscard]]}}<br>{{codecpp|[[nodiscard("reason")]]}}
|| Issues a compiler warning if the return value of the marked symbol is discarded or ignored for the reason specified (if given).
|-
| {{codecpp|[[likely]]}}<br>{{codecpp|[[unlikely]]}}
|| 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).
|-
| {{codecpp|[[no_unique_address]]}}
|| Indicates that a non-static data member need not have an address distinct from all other non-static data members of its class.
|-
| {{codecpp|[[assume(expression)]]}}
|| Indicates that the given expression always evaluates to <code>{{cpp|true</code>}} at a given point, allowing the compiler to make optimisations based on that information.
|-
| {{codecpp|[[indeterminate]]}}
|| Indicates that an object bears an indeterminate value if it is not initialised.
|-
| {{codecpp|[[unsequenced]]}}
|| Indicates that a function is stateless, effectless, idempotent and independent.
|-
| {{codecpp|[[reproducible]]}}
|| Indicates that a function is effectless and idempotent.
|}
 
=== Scoped attributes ===
As mentioned previously, GCC and Clang have scoped (namespaced) attributes, such as {{codecpp|[[gnu::always_inline]]}}, {{codecpp|[[gnu::hot]]}}, and {{codecpp|[[gnu::const]]}}. To apply multiple scoped attributes, one may write:
<syntaxhighlight lang="cpp">
[[gnu::always_inline]] [[gnu::hot]] [[gnu::const]] [[nodiscard]]