4.6 KiB
[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); `
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.
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.
Throws: meta::exception unlesstempl represents a template, and every reflection in arguments represents a construct usable as a template argument ([temp.arg]).
[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.13 Reflection substitution [meta.reflection.substitute]") R = initializer_list<info>> consteval info substitute(info templ, R&& arguments);
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.
Returns: ^^Z<[:Args:]...>.
Throws: meta::exception unlesscan_substitute(templ, arguments) is true.
[Note 2:
If forming Z<[:Args:]...> leads to a failure outside of the immediate context, the program is ill-formed.
â end note]
[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]
[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]