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

122 lines
3.8 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.result]
# 21 Metaprogramming library [[meta]](./#meta)
## 21.4 Reflection [[meta.reflection]](meta.reflection#result)
### 21.4.14 Expression result reflection [meta.reflection.result]
[🔗](#lib:reflect_constant)
`template<class T>
consteval info reflect_constant(T expr);
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6097)
*Mandates*: is_copy_constructible_v<T> is true and T is a cv-unqualified structural type ([[temp.param]](temp.param "13.2Template parameters"))
that is not a reference type[.](#1.sentence-1)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6103)
Let V be:
- [(2.1)](#2.1)
if T is a class type,
then an object that is template-argument-equivalent to the value of expr;
- [(2.2)](#2.2)
otherwise, the value of expr[.](#2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6113)
*Returns*: template_arguments_of(^^TCls<V>)[0],
with TCls as defined below[.](#3.sentence-1)
[*Note [1](#note-1)*:
This is a reflection of an object for class types,
and a reflection of a value otherwise[.](#3.sentence-2)
— *end note*]
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6122)
*Throws*: meta::exception unless
the [*template-id*](temp.names#nt:template-id "13.3Names of template specializations[temp.names]") TCls<V> would be valid
given the invented templatetemplate<T P> struct TCls;
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6131)
[*Example [1](#example-1)*: template<auto D>struct A { };
struct N { int x; };struct K { char const* p; };
constexpr info r1 = reflect_constant(42);static_assert(is_value(r1));static_assert(r1 == template_arguments_of(^^A<42>)[0]);
constexpr info r2 = reflect_constant(N{42});static_assert(is_object(r2));static_assert(r2 == template_arguments_of(^^A<N{42}>)[0]);
constexpr info r3 = reflect_constant(K{nullptr}); // OKconstexpr info r4 = reflect_constant(K{"ebab"}); // error: constituent pointer// points to string literal — *end example*]
[🔗](#lib:reflect_object)
`template<class T>
consteval info reflect_object(T& expr);
`
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6162)
*Mandates*: T is an object type[.](#6.sentence-1)
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6166)
*Returns*: A reflection of the object designated by expr[.](#7.sentence-1)
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6170)
*Throws*: meta::exception unlessexpr is suitable for use as a constant template argument
for a constant template parameter of type T& ([[temp.arg.nontype]](temp.arg.nontype "13.4.3Constant template arguments"))[.](#8.sentence-1)
[🔗](#lib:reflect_function)
`template<class T>
consteval info reflect_function(T& fn);
`
[9](#9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6184)
*Mandates*: T is a function type[.](#9.sentence-1)
[10](#10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6188)
*Returns*: A reflection of the function designated by fn[.](#10.sentence-1)
[11](#11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/meta.tex#L6192)
*Throws*: meta::exception unlessfn is suitable for use as a constant template argument
for a constant template parameter of type T& ([[temp.arg.nontype]](temp.arg.nontype "13.4.3Constant template arguments"))[.](#11.sentence-1)