◐ Shell
clean mode source ↗

Annotations (since C++26) - cppreference.com

From cppreference.com

Attach information to declarations in a way that reflection can observe.

Syntax

The list of annotations is introduced in an attribute specifier. Each annotation starts with =.

= constant-expression

Explanation

An annotation may be applied to any declaration of a type, type alias, variable, function, function parameter of non-void type, namespace, enumerator, base-specifier, or non-static data member.

Let E be std::meta::reflect_constant(expr) (where expr is the constant-expression), E must be a constant expression. The result of E can be observed by calling std::meta::constant_of on the reflection of the annotation.

Annotations are always distinct from one another, even if they have equivalent values and are applied to the same declaration.

[[=42, =42]] int x;
static_assert(std::meta::annotations_of(^^x).size() == 2);

[[=42]] int f();
[[=24]] int f();
static_assert(std::meta::annotations_of(^^f).size() == 2);

Substituting into an annotation is not an immediate context.

Example

See also