0% found this document useful (0 votes)
17 views3 pages

Reflective Programming - Difference Between Revisions - Wikipedia

The document discusses a code revision related to C++26, focusing on the implementation of reflection and method invocation. It includes templates for creating invokers for non-static methods and demonstrates the use of meta-programming techniques. The provided code snippets illustrate how to find and invoke methods dynamically using reflection in C++.

Uploaded by

Iqra Mimo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Reflective Programming - Difference Between Revisions - Wikipedia

The document discusses a code revision related to C++26, focusing on the implementation of reflection and method invocation. It includes templates for creating invokers for non-static methods and demonstrates the use of meta-programming techniques. The provided code snippets illustrate how to find and invoke methods dynamically using reflection in C++.

Uploaded by

Iqra Mimo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Revision as of 18:21, 21 June 2025 edit Latest revision as of 10:36, 3 July 2025 edit

24 days ago 13 days ago


2605:8d80:1391:c3e:2051:d7fe:2fa:a194 (talk) undo
→JavaScript and TypeScript Moondezmon (talk | contribs)
← Previous edit m make the C++26 example standard
compliant

Line 73:

import std;

using Info = std::meta::info;


using String = std::string views::filter;

consteval bool is_nonstatic_method_fn(info mem) {


template <typename T>
return is_class_member(mem)
using AlwaysFalse = std::always_false<T>;
&& !is_static_member(mem)
template <typename T, T v>

&& is_function(mem);
using IntegralConstant = std::integral_constant<T, v>;
}
template <typename T>
↲ newline
using Vector = std::vector<T>;
consteval info find_method(info ty, const char* name) {
constexpr auto ctx = std::meta::access_context::current();
for (info mem : members_of(ty, ctx) |
filter(is_nonstatic_method_fn))
if (std::meta::identifier_of(member mem) ==
methodName name) {

} return mem;
return info{};
}
↲ newline

template < typename info T Ty , T auto v Name >


constexpr auto create_invoker_impl = []{
using Type = [: Ty :];
static constexpr info M = find_method(Ty, Name);
static_assert(parameters_of(M).size() == 0 && return_type_of(M) ==
^^void);
return [](Type& instance) { instance.[: M :](); };
}();

consteval auto info makeInvoker create_invoker(Info& info refl ty,


const std::string_view String& methodName name ) {
return [refl] substitute(auto&& instance) ^^create_invoker_impl, {
constexpr Vector<Info> members =
std::meta::nonstatic_members_of reflect_constant(refl ty); ,
std::meta::reflect_constant_string(name)});
for (Info& member: members) {

if (std::meta::identifier_of(member) == methodName) {
return [&]<auto M>(IntegralConstant<decltype(member),
M>) {
instance.[:M: ]();
}(IntegralConstant<decltype(member), member>{});

}
}
static_assert(AlwaysFalse<decltype(instance)>(), "Method not
found");
};
}

Line 104 ⟶ 111:

// With reflection
constexpr auto reflFoo invokePrint = [: create_invoker(^^Foo,
"printHello") :];
auto invokePrint = makeInvoker(reflFoo, "printHello");
invokePrint(foo);

You might also like