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

4.6 KiB
Raw Permalink Blame History

[meta.reflection.substitute]

21 Metaprogramming library [meta]

21.4 Reflection [meta.reflection]

21.4.13 Reflection substitution [meta.reflection.substitute]

🔗

`template concept reflection_range = ranges::input_range && same_as<ranges::range_value_t, info> && same_as<remove_cvref_t<ranges::range_reference_t>, info>;

template<reflection_range R = initializer_list> consteval bool can_substitute(info templ, R&& arguments); `

1

#

Let Z be the template represented by templ and let Args... be a sequence of prvalue constant expressions that compute the reflections held by the elements of arguments, in order.

2

#

Returns: true if Z<[:Args:]...> is a valid template-id ([temp.names]) that does not name a function whose type contains an undeduced placeholder type.

Otherwise, false.

3

#

Throws: meta::exception unlesstempl represents a template, and every reflection in arguments represents a construct usable as a template argument ([temp.arg]).

4

#

[Note 1:

If forming Z<[:Args:]...> leads to a failure outside of the immediate context, the program is ill-formed.

— end note]

🔗

template<[reflection_range](#concept:reflection_range "21.4.13Reflection substitution[meta.reflection.substitute]") R = initializer_list<info>> consteval info substitute(info templ, R&& arguments);

5

#

Let Z be the template represented by templ and let Args... be a sequence of prvalue constant expressions that compute the reflections held by the elements of arguments, in order.

6

#

Returns: ^^Z<[:Args:]...>.

7

#

Throws: meta::exception unlesscan_substitute(templ, arguments) is true.

8

#

[Note 2:

If forming Z<[:Args:]...> leads to a failure outside of the immediate context, the program is ill-formed.

— end note]

9

#

[Example 1: templateauto fn1();

static_assert(!can_substitute(^^fn1, {^^int})); // OKconstexpr info r1 = substitute(^^fn1, {^^int}); // error: fn contains an undeduced// placeholder type for its return typetemplateauto fn2() {static_assert(^^T != ^^int); // static assertion failed during instantiation of fnreturn 0; }constexpr bool r2 = can_substitute(^^fn2, {^^int}); // error: instantiation of body of fn// is needed to deduce return type — end example]

10

#

[Example 2: consteval info to_integral_constant(unsigned i) {return substitute(^^integral_constant, {^^unsigned, reflect_constant(i)});}constexpr info r = to_integral_constant(2); // OK, r represents the type// integral_constant<unsigned, 2> — end example]