21 Metaprogramming library [meta]

21.4 Reflection [meta.reflection]

21.4.17 Annotation reflection [meta.reflection.annotation]

consteval vector<info> annotations_of(info item);
Constant When: item represents a type, type alias, variable, function, namespace, enumerator, direct base class relationship, or non-static data member.
Let E be
  • the corresponding base-specifier if item represents a direct base class relationship,
  • otherwise, the entity represented by item.
Returns: A vector containing all of the reflections R representing each annotation applying to each declaration of E that precedes either some point in the evaluation context ([expr.const]) or a point immediately following the class-specifier of the outermost class for which such a point is in a complete-class context.
For any two reflections and in the returned vector, if the annotation represented by precedes the annotation represented by , then appears before .
If and represent annotations from the same translation unit T, any element in the returned vector between and represents an annotation from T.
[Note 1: 
The order in which two annotations appear is otherwise unspecified.
— end note]
[Example 1: [[=1]] void f(); [[=2, =3]] void g(); void g [[=4]] (); static_assert(annotations_of(^^f).size() == 1); static_assert(annotations_of(^^g).size() == 3); static_assert([: constant_of(annotations_of(^^g)[0]) :] == 2); static_assert(extract<int>(annotations_of(^^g)[1]) == 3); static_assert(extract<int>(annotations_of(^^g)[2]) == 4); struct Option { bool value; }; struct C { [[=Option{true}]] int a; [[=Option{false}]] int b; }; static_assert(extract<Option>(annotations_of(^^C::a)[0]).value); static_assert(!extract<Option>(annotations_of(^^C::b)[0]).value); template<class T> struct [[=42]] D { }; constexpr std::meta::info a1 = annotations_of(^^D<int>)[0]; constexpr std::meta::info a2 = annotations_of(^^D<char>)[0]; static_assert(a1 != a2); static_assert(constant_of(a1) == constant_of(a2)); [[=1]] int x, y; static_assert(annotations_of(^^x)[0] == annotations_of(^^y)[0]); — end example]
consteval vector<info> annotations_of_with_type(info item, info type);
Constant When:
  • annotations_of(item) is a constant expression and
  • dealias(type) represents a type that is complete from some point in the evaluation context.
Returns: A vector containing each element e of annotations_of(item) where remove_const(type_of(e)) == remove_const(type) is true, preserving their order.