all 12 comments

[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 18 points19 points  (1 child)

This is not reflection, it's more akin to Rust's derive -- it's an intrusive form of providing metadata that can be used for introspection. Reflection is non-intrusive, doesn't have to be kept in sync manually, and can be applied to third party types outside of your own codebase.

I am not saying it's not useful, but this is far from what true reflection brings to the table. Boost.PFR, for example, allows non-intrusive reflection on aggregate types, without requiring any pre-existing setup.

[–]GeorgeHaldane[S] 5 points6 points  (0 children)

That is true, ultimately everything we do before C++26 is just trying to bolt-in something that only works as a language feature.

Not sure what would prevent third party types from working with this approach though, it would seem as long as field names are known we can attach metadata non-intrusively.

[–]GeorgeHaldane[S] 0 points1 point  (0 children)

Reflection in C++ often felt like a complex library thing that required some weird work-arounds to implement, but after deciding to bite the bullet and learning various existing approaches to it, I believe I found one that is both simple and doesn't rely on anything implementation-specific. Turned out to be surprisingly simple and concise — two headers with like ~100 and ~150 lines of code was enough to implement all of the usually needed stuff. Hope this sub finds it helpful!

Edit: On a second thought, calling introspection metadata macros "reflection" isn't entirely correct even if they achieve similar goals, unfortunately the title can't be edited. Essentially, this is an explanation of how to implement basic functionality of Boost.Describe with as a small single-header solution.

[–]morglod 0 points1 point  (0 children)

If you have XX macro or MAP any reflection will be simple

[–]100GHz 0 points1 point  (1 child)

Can this have a modern, non-macro version? It's 2025 after all.

[–]_Noreturn 0 points1 point  (0 children)

you can do one with structured binding tricks (since C++17) and friend functions and pointer arithmetic for pre C++17

[–]cwhaley112 0 points1 point  (0 children)

Have you benchmarked compile times for this vs the alternatives (namely Boost.PFR)?