Skip to content

Instantly share code, notes, and snippets.

@OperationDarkside
Created January 20, 2026 23:31
Show Gist options
  • Select an option

  • Save OperationDarkside/aaa74bfe0379068b858f7075f04517c9 to your computer and use it in GitHub Desktop.

Select an option

Save OperationDarkside/aaa74bfe0379068b858f7075f04517c9 to your computer and use it in GitHub Desktop.
Getting around the C++26 reflection and "template for" consteval restrictions for iterating over members in runtime functions
#include <meta>
#include <iostream>
#include <span>
#include <array>
namespace db {
struct Data {
int a;
int b;
int c;
};
struct Dataa {
int d;
int e;
int f;
};
struct Dataaa {
int g;
int h;
int i;
};
}
template<std::size_t N>
constexpr auto make_num_array() {
std::array<std::size_t, N> a{};
for(int i = 0; i < N; i++) {
a[i] = i;
}
return a;
}
int main(){
db::Data d{1,2,3};
constexpr auto ctx = std::meta::access_context::current();
constexpr static auto ns_members = std::define_static_array(std::meta::members_of(^^db, ctx));
constexpr std::size_t N = ns_members.size();
constexpr static auto ns_members_indices = make_num_array<N>();
template for (constexpr auto i : ns_members_indices) {
constexpr auto clss = ns_members[i];
constexpr auto clss_name = std::meta::identifier_of(clss);
std::cout << clss_name << " {\n";
constexpr static auto class_members = std::define_static_array(std::meta::nonstatic_data_members_of(clss, ctx));
constexpr static auto class_members_indices = make_num_array<class_members.size()>();
template for (constexpr auto j : class_members_indices) {
constexpr auto m = class_members[j];
constexpr auto class_member_name = std::meta::identifier_of(m);
constexpr auto class_member_type_name = std::meta::display_string_of(std::meta::type_of(m));
std::cout << "\t" << class_member_type_name << " " << class_member_name << ";\n";
}
std::cout << "}\n";
}
std::cout << "}\n";
template for (auto& m : d) {
constexpr auto bla = std::meta::identifier_of(^^m);
constexpr auto blubb = std::meta::display_string_of(std::meta::type_of(^^m));
std::cout << bla << " - " << blubb << "\n";
m = 5;
}
std::cout << d.a << " - " << d.b << " - " << d.c << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment