MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/cpp/comments/1t25byx/post_examples_of_using_reflections_in_your/on46qg9/?context=3
r/cpp • u/enl1l • May 02 '26
What the title says. I just want to see what interesting things people are using reflection for now that its in gcc. Thanks.
25 comments sorted by
View all comments
1
Nested and dynamically evaluated yaml config loader:
template<typename T>template<typename T> concept YamlConfig = requires(T x, const YAML::Node& n, std::map<std::string, double>& ctx) { x.fromYaml(n, ctx); }; template<typename T> struct ManagedConfig { void fromYaml(const YAML::Node& node, std::map<std::string, double>& context, std::string_view basePath = "") { constexpr auto members = std::define_static_array( std::meta::nonstatic_data_members_of(^^T, std::meta::access_context::unchecked()) ); template for (constexpr auto m : auto(members)) { using U = [:std::meta::type_of(m):]; constexpr auto name = std::meta::identifier_of(m); auto fieldNode = node[name]; if (!fieldNode.IsDefined()) { continue; } auto& member = static_cast<T*>(this)->[:m:]; if constexpr (YamlConfig<U>) { member.fromYaml(fieldNode, context, name); } else if constexpr (std::is_convertible_v<U, double>) { auto value = misc::evaluateExpression<U>(fieldNode.template as<std::string>(), context); context[std::format("{}_{}", basePath, name)] = static_cast<double>(value); member = value; } } } }; struct Cfg2 : public ManagedConfig<Cfg2> { double b; }; struct Cfg : public ManagedConfig<Cfg> { double a; Cfg2 cfg2; }; Cfg cfg; cfg.fromYaml(...);
View all comments
1
u/Huge-Presentation810 May 21 '26
Nested and dynamically evaluated yaml config loader: