Namespaces
Variants

Annotations (since C++26)

From cppreference.net
 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 

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

obtains the annotations applying to the reflected entity
(function)
obtains the annotations that apply to the reflected entity and have the specified type
(function)