Get the latest tech news
Type-based vs. Value-based Reflection
Frequently, whenever the topic of Reflection comes up, I see a lot of complains specifically about the new syntax being added to support Reflection in C++26. I’ve always thought of that as being largely driven by unfamiliarity — this syntax is new, unfamiliar, and thus bad. I thought I’d take a different tactic in this post: let’s take a problem that can only be solved with Reflection and compare what the solution would look like between:
The other thing to notice is that we had to use a metafunction to pull out the first element in the type-based model, but in the value-based one we didn’t have to use a dedicated reflection function — we were able to just used the index operator. The solution isn’t just half as long, it’s also significantly less complicated, and doesn’t require an extra library specifically tailored to solve the problem. In the value-based model, we require novel syntax — but it’s significantly terser, works for all non-static data member kinds, and also makes the fact that we’re coming out of the reflection domain much clearer.
Or read this on Hacker News