Content deleted Content added
No edit summary |
|||
Line 601:
std::println("Hello, {}!", me);
}
</syntaxhighlight>
== 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 <code>[[gnu::*]]</code>.
One may apply multiple attributes as a list, for instance <code>[[A, B, C]]</code> (where <code>A</code>, <code>B</code>, and <code>C</code> are attributes). Furthermore, attributes may also accept arguments, like <code>[[A("This is a parameter")]]</code>.
=== Standard attributes ===
The C++ standard defines the following attributes:
Legend:<br>
{{color box|#E6E6B7}}: Deprecated<br>
{{color box|#F99}}: Removed
{| class="wikitable"
! Name !! Description
|-
| {{code|[[noreturn]]}}
|| Indicates that the specified function will not return to its caller.
|-
| style="background:#F99" | {{code|[[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.
|-
| {{code|[[deprecated]]}}<br>{{code|[[deprecated("reason")]]}}
|| Indicates that the use of the marked symbol is allowed but discouraged/deprecated for the reason specified (if given).
|-
| {{code|[[fallthrough]]}}
|| Indicates that the fall through from the previous case label is intentional.
|-
| {{code|[[maybe_unused]]}}
|| Suppresses compiler warnings on an unused entity.
|-
| {{code|[[nodiscard]]}}<br>{{code|[[nodiscard("reason")]]}}
|| Issues a compiler warning if the return value of the marked symbol is discarded or ignored for the reason specified (if given).
|-
| {{code|[[likely]]}}<br>{{code|[[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).
|-
| {{code|[[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.
|-
| {{code|[[assume(expression)]]}}
|| Indicates that the given expression always evaluates to <code>true</code> at a given point, allowing the compiler to make optimisations based on that information.
|-
| {{code|[[indeterminate]]}}
|| Indicates that an object bears an indeterminate value if it is not initialised.
|-
| {{code|[[unsequenced]]}}
|| Indicates that a function is stateless, effectless, idempotent and independent.
|-
| {{code|[[reproducible]]}}
|| Indicates that a function is effectless and idempotent.
|}
=== Scoped attributes ===
As mentioned previously, GCC and Clang have scoped (namespaced) attributes, such as <code>[[gnu::always_inline]]</code>, <code>[[gnu::hot]]</code>, and <code>[[gnu::const]]</code>. To apply multiple scoped attributes, one may write:
<syntaxhighlight lang="cpp">
[[gnu::always_inline]] [[gnu::hot]] [[gnu::const]] [[nodiscard]]
inline int f(); // declare f with four attributes
[[gnu::always_inline, gnu::const, gnu::hot, nodiscard]]
int f(); // same as above, but uses a single attr specifier that contains four attributes
// C++17:
[[using gnu : const, always_inline, hot]] [[nodiscard]]
int f[[gnu::always_inline]](); // an attribute may appear in multiple specifiers
</syntaxhighlight>
|