Files
2025-10-25 03:02:53 +03:00

104 lines
3.6 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[meta.reflection.annotation]
# 21 Metaprogramming library [[meta]](./#meta)
## 21.4 Reflection [[meta.reflection]](meta.reflection#annotation)
### 21.4.18 Annotation reflection [meta.reflection.annotation]
[🔗](#lib:annotations_of)
`consteval vector<info> annotations_of(info item);
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6930)
Let E be
- [(1.1)](#1.1)
the corresponding [*base-specifier*](class.derived.general#nt:base-specifier "11.7.1General[class.derived.general]") if item represents a direct base class relationship,
- [(1.2)](#1.2)
otherwise, the entity represented by item[.](#1.sentence-1)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6940)
*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]](expr.const "7.7Constant expressions")) or
a point immediately following the [*class-specifier*](class.pre#nt:class-specifier "11.1Preamble[class.pre]") of the outermost class for which such a point is in a complete-class context[.](#2.sentence-1)
For any two reflections R1 and R2 in the returned vector,
if the annotation represented by R1 precedes the annotation represented by R2,
then R1 appears before R2[.](#2.sentence-2)
If R1 and R2 represent annotations from the same translation unit T,
any element in the returned vector between R1 and R2 represents an annotation from T[.](#2.sentence-3)
[*Note [1](#note-1)*:
The order in which two annotations appear is otherwise unspecified[.](#2.sentence-4)
— *end note*]
[*Example [1](#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*]
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6991)
*Throws*: meta::exception unlessitem represents a
type,
type alias,
variable,
function,
namespace,
enumerator,
direct base class relationship, or
non-static data member[.](#3.sentence-1)
[🔗](#lib:annotations_of_with_type)
`consteval vector<info> annotations_of_with_type(info item, info type);
`
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L7011)
*Returns*: A vector containing each element e of annotations_of(item) whereremove_const(type_of(e)) == remove_const(type) is true, preserving their order[.](#4.sentence-1)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L7020)
*Throws*: meta::exception unless
- [(5.1)](#5.1)
annotations_of(item) is a constant expression and
- [(5.2)](#5.2)
dealias(type) represents a type that is complete
from some point in the evaluation context[.](#5.sentence-1)